Python input(): Getting User's Input

The input() function is a part of the core library of standard Python distribution. It reads the key strokes as a string object which can be referred to by a variable having a suitable name.

Taking User's Input in Python
Taking User's Input

Note that the blinking cursor waits for the user's input. The user enters his input and then hits Enter. This will be captured as a string.

In the above example, the input() function takes the user's input from the next line, e.g. 'Steve' in this case. input() will capture it and assign it to a name variable. The name variable will display whatever the user has provided as the input.

The input() function has an optional string parameter that acts as a prompt for the user.

Taking User's Input in Python
Taking User's Input

The input() function always reads the input as a string, even if comprises of digits. The type() function used earlier confirms this behaviour.

Example: Multi-line Comments
>>> name=input("Enter your name: ")
Enter your name: Steve                      
>>> type(name)                     
<class 'str'>                         
>>> age=input("Enter your age: ")  
Enter your age: 21                          
>>> type(age)                      
<class 'str'>