Python @ DjangoSpin

Python: Getting user input - builtin function input()

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Getting user input in Python

Getting user input in Python

The builtin input() is used for receiving input from the user. The input() function takes an argument, which is what will be prompted on the screen before the user enters a value. We can assign the value entered to a variable that we can use later in the program.

>>> input("Tell me your name. \n")
Tell me your name.
Tyrion Lannister                 # User types this and presses enter
'Tyrion Lannister'
 
>>> name = input("Tell me your name. \n")
Tell me your name.
Tyrion Lannister
>>> print("Did you just say " + name + "?")
Did you just say Tyrion Lannister?

It is worth emphasizing that the input function returns a string, so if you require it to be some other data type, it needs to be converted into the required data type using Type Casting.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply