Taking user input in python
Programmers and developers might often need a data to be entered by the user. Like you might have seen many websites asking for your username and password for logging in such Facebook, Twitter, Gmail, etc. You need to enter your details to use these websites.This is just one application of importance of user inputs. This will stop you at a certain point and will move forward when you enter details
To take user input from keyboard we just need to use an inbuilt function input() in our program
Note: Here we are discussing python 3.0 and above .The older versions have some different format so don't confuse with them
SYNTAX:
input ( prompt )
eg.
Output:
Enter your username
mohit@yopmail.com
Enter your password
123+-[]@
Your username is : mohit@yopmail.com Your password is :123+-[]@
Or instead of using print (for specifying which input you want to take from user), you can simply use a statement inside input() function as specified in the example:
eg.
Output:
Enter your username ankit@hotmail.com
Enter your password ankit1234@#
Your username is ankit@hotmail.com Your username is ankit1234@#
To take user input from keyboard we just need to use an inbuilt function input() in our program
Note: Here we are discussing python 3.0 and above .The older versions have some different format so don't confuse with them
SYNTAX:
input ( prompt )
eg.
print("Enter your username") username=input() print("Enter your password") password=input() print("Your username is :",username) print("Your password is :",password)
Output:
Enter your username
mohit@yopmail.com
Enter your password
123+-[]@
Your username is : mohit@yopmail.com Your password is :123+-[]@
Or instead of using print (for specifying which input you want to take from user), you can simply use a statement inside input() function as specified in the example:
eg.
username = input("Enter your username") password = input("Enter your password") print("Your username is", username) print("Your username is", password)
Output:
Enter your username ankit@hotmail.com
Enter your password ankit1234@#
Your username is ankit@hotmail.com Your username is ankit1234@#
Remember that the input() function takes input in the form of string.Numbers can also be taken as strings.It is illustrated in below example:
eg.
a=input("Enter first value ") b=input("Enter second value ") c=input("Enter third value ") print("Output is",a+b+c) #Strings get concatenated when + operator is used
Output:
Enter first value 45
Enter second value 67
Enter third value 90
Output is 456790
We can be double sure about it if we use type() function for checking type of variables.
a=input("Enter first value") b=input("Enter second value") c=input("Enter third value") print("Output is",a+b+c) print(type(a)) print(type(b)) print(type(c))
Output:
Enter first value hii
Enter second value67.89
Enter third value][\\
Output is hii 67.89][\\
<class 'str'>
<class 'str'>
<class 'str'>
Printing value multiple times :
a=input("Enter first value ") b=input("Enter second value ") c=input("Enter third value ") n=10print((a+b+c+"\n")*10)
Output:
Enter first value Hey!
Enter second value Subscribe
Enter third value this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Hey! Subscribe this blog
Comments
Post a Comment
Please do not enter any spam link in the comment box