Python user Input and Print function

 


Input and Print function in Python

input() :
  • this function is used to collect input from user at run time

#program to run time data from user
name = input("Enter name: ")

#output
Enter name:

  • lets provide input

Enter name: Techno Xpresss


print() :
  • this function is used to print data on console/output window

#program to print data on console
print("Hey let's learn Python")

#output
Hey let's learn Python


Example :

#program to take user input and print on console

name = input("Enter name: ")
print("Hey", name, "let's learn Python")


#output
Enter name: Techno Xpresss
Hey Techno Xpresss let's learn Python



But input() has its own limitation while collecting input at run time, let's have a look at the below example

#program to take input from user and perform certain action

a = input("enter num1 : ")
b =
input("enter num2 : ")
c = a + b

print ("a:", a)
print ("b:", b)
print ("c:", c)

our expectation from above example, 
  • at run time, it will ask user to provide 2 numbers which we are storing in a and b.
  • suppose user entered a as 5 and b as 10
  • then printing a, b and c(sum of a and b)
  • expected output is
    • a is 5
    • bis 10
    • c is 15
 #output
enter a: 5
enter b: 10
a: 5
b: 10
c: 510

a and b are as expected but not c.


Limitation of input()
Example:

#program to take input from user and perform certain action

name=input("Enter name : ")
age =
input("Enter age in years : ")
marks =
input("Enter % scored :")
print ("Hey",name,"you are",age,"years old and you scored",marks, "% marks")


#output
Enter name : Techno Xpresss
Enter age
in years : 28
Enter % scored :80.72
Hey Techno Xpresss you are 28 years old and you scored 80.72 % marks

so above example is needed to enter few values from user and expectation is
  • name   : string
  • age      : integer
  • marks  : float
Hey Techno Xpresss you are 28 years old and you scored 80.72 % marks
this above message from output looks meaningful

lets see what type of data is assigned to name , age and marks


#program to take input from user to understand type of data input function returns

name=input("Enter name : ")
age =
input("Enter age in years : ")
marks =
input("Enter % scored :")
print ("Hey",name,"you are",age,"years old and you scored",marks, "% marks")
print("typeof(name)",type(name), "\ntypeof(age)",type(age), "\ntypeof(marks)",type(marks))

 #output
Enter name : Techno Xpress
Enter age
in years : 28
Enter % scored :80.72
Hey Techno Xpress you are 28 years old and you scored 80.72 % marks

typeof(name) <
class 'str'>
typeof(age) <
class 'str'>
typeof(marks) <
class 'str'>



It is clearly visible that input returns the data as a 'str class' object. 
This is the reason that when we tried to add 5 and 10 above, instead of 15 we got 510 (as a and b were collected as string and when we add then, b got cascaded to a)

input() always return used provided data as a string.
So to collect data as integer or float, we have to do typecasting





Comments

My photo
Techno Xpresss
Bangalore, Karnataka, India