Python User Input

Python has pre defined built-in input() method to receive user input.

Syntax of input()


Here, prompt is the default message to write before user input.

Pyhton User Input Example
>>> val = input("Enter the number: ")
Enter the number: 3232
>>> print("Number is ",val)
('Number is ', 3232)
>>> print("Type ",type(val))
('Type ', <type 'int'>)

The second line is entered by the user. This value is stored in val variable. In the last line, we have used the type() operator to print the data type of the user input.

Here, we have stored the program in '' variable and run using Python Shell.

Python user input

Output of the above code
Python user input

You can also write the above program as -

x = input("Please enter your name: ")
print("Hello ",x)
print("Type ", type(x))

The input function produces only string. But you can convert it into an integer using int() function. We can directly convert the user input in integer value, like -

x = int(input("Please enter an integer value:")) 
print("Number is ",x)

Read more articles

General Knowledge

Learn Popular Language