User Input

suggest change

Interactive input

In Python 3, input reads a line and returns a str. (In Python 2, input evaluated the line as code; that behavior was removed.)

name = input("What is your name? ")
# Out: What is your name? _

The function takes a string argument, which displays it as a prompt and returns a string. The above code provides a prompt, waiting for the user to input.

name = input("What is your name? ")
# Out: What is your name?

If the user types “Bob” and hits Enter, the variable name will hold the string "Bob":

name = input("What is your name? ")
# Out: What is your name? Bob
print(name)
# Out: Bob

Note that the input is always of type str, which is important if you want the user to enter numbers. Therefore, you need to convert the str before trying to use it as a number:

x = input("Write a number:")
# Out: Write a number: 10
x / 2
# Out: TypeError: unsupported operand type(s) for /: 'str' and 'int'
float(x) / 2
# Out: 5.0

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents