Input
The input() function asks the user to type something and returns what they typed as a string.
Example
name = input("What is your name? ")
print("Hello, " + name)
When run:
What is your name? Alex
Hello, Alex
How It Works
- Python displays the prompt text (the string inside the parentheses)
- The program pauses and waits for the user to type something
- When the user presses Enter, their input is returned as a string
Store Input in a Variable
Always store user input in a variable so you can use it later:
city = input("Where are you from? ")
food = input("What's your favorite food? ")
print(city + " has great " + food)
Input Always Returns a String
Even if the user types a number, input() returns it as a string:
age = input("Enter your age: ")
print(type(age)) # <class 'str'>
To use the input as a number, convert it:
age = input("Enter your age: ")
age = int(age) # convert to integer
print(age + 1) # now math works
price = input("Enter price: ")
price = float(price) # convert to decimal
print(price * 1.08) # calculate with tax
See the Data Types guide for more on type conversion.
Empty Prompts
You can use an empty string if you don't want a prompt:
print("Enter your name:")
name = input("")
But it's usually clearer to include the prompt directly:
name = input("Enter your name: ")
Tip: add a space at the end of your prompt so the user's typing doesn't bump right against your text.