How to Get User Input in Python
If you want your Python program to ask the user for a name, age, or any other value, use input().
This page shows you how to:
- ask for input with
input() - store the result in a variable
- print the value back to the user
- convert input text to numbers when needed
- avoid common beginner mistakes
Quick answer
name = input("Enter your name: ")
print("Hello,", name)
input() always returns text. If you need a number, convert it with int() or float().
What this page helps you do
- Use
input()to read text from the keyboard - Store the result in a variable
- Print the entered value
- Convert input text to numbers when needed
Basic syntax of input()
input() pauses the program and waits for the user to type something.
The text inside input("...") is called the prompt. It tells the user what to enter.
The value returned by input() is always a string. A string is text in Python.
Basic example:
name = input("Enter your name: ")
print(name)
What happens here:
input("Enter your name: ")shows a message- the program waits for the user to type
- the typed value is stored in
name print(name)shows the value
If you want a full explanation of how this function works, see Python input() function explained.
Get text input
Use input() directly when you want text such as:
- names
- cities
- favorite colors
- yes/no answers
Example:
city = input("Which city do you live in? ")
print("You live in", city)
Possible output:
Which city do you live in? New York
You live in New York
If the user types spaces, they stay in the result.
Example:
full_name = input("Enter your full name: ")
print(full_name)
If the user enters Ava Smith, the variable contains the full text Ava Smith.
Convert input to a number
input() does not return numbers by itself. Even if the user types 25, Python still treats it as text.
Use int() for whole numbers
age = int(input("Enter your age: "))
print("Next year you will be", age + 1)
Possible output:
Enter your age: 30
Next year you will be 31
Use float() for decimal numbers
price = float(input("Enter the price: "))
print("The price is", price)
Possible output:
Enter the price: 19.99
The price is 19.99
Use:
If you want more examples, see how to convert user input to numbers in Python.
Important warning
If the user enters something that cannot be converted, Python raises a ValueError.
Example:
age = int(input("Enter your age: "))
print(age)
If the user types hello, the program crashes because hello is not a valid integer.
For that specific problem, see how to fix ValueError: invalid literal for int() with base 10.
Use input in a simple program
Here is a small program that asks for two values and prints a message.
name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hello,", name)
print("Next year you will be", age + 1)
Example run:
What is your name? Maya
How old are you? 12
Hello, Maya
Next year you will be 13
This example combines:
- prompts
- variables
- number conversion
print()
Handle invalid input safely
If you convert user input with int() or float(), bad input can cause an error.
A simple way to handle this is with try and except.
try:
age = int(input("Enter your age: "))
print("Next year you will be", age + 1)
except ValueError:
print("Please enter a valid whole number.")
What this does:
tryruns the code that might fail- if
int()cannot convert the text,except ValueErrorruns - the program shows a clear message instead of crashing
Example run with bad input:
Enter your age: twelve
Please enter a valid whole number.
If you are new to this, see how to use try and except blocks in Python.
Common beginner mistakes
Forgetting that input() returns a string
This is one of the most common mistakes.
age = input("Enter your age: ")
print(age + 1)
This does not work because age is text, not a number.
Fix it by converting the input:
age = int(input("Enter your age: "))
print(age + 1)
Trying to add a number to raw input text
If you mix strings and integers incorrectly, Python raises an error.
Wrong:
score = input("Enter your score: ")
print("Your score plus one is " + (score + 1))
Right:
score = int(input("Enter your score: "))
print("Your score plus one is", score + 1)
Using input() without saving the result
If you do not store the result, you cannot use it later.
Less useful:
input("Enter your name: ")
print("Hello")
Better:
name = input("Enter your name: ")
print("Hello,", name)
Writing a prompt that is unclear
A good prompt tells the user exactly what to enter.
Unclear:
value = input("Enter value: ")
Clearer:
value = input("Enter your age in whole numbers: ")
Leading or trailing spaces in input
Sometimes the user accidentally types extra spaces.
You can check this with:
user_input = input("Enter something: ")
print(user_input)
print(type(user_input))
print(repr(user_input))
These lines help you debug:
print(user_input)shows the visible valueprint(type(user_input))shows the data typeprint(repr(user_input))shows hidden spaces like' hello '
If needed, you can remove extra spaces with .strip():
name = input("Enter your name: ").strip()
print("Hello,", name)
FAQ
What does input() return in Python?
It returns a string, even if the user types digits.
How do I get a number from user input?
Wrap input() with int() for whole numbers or float() for decimals.
Why does my program crash when I type letters instead of a number?
int() and float() raise ValueError if the text cannot be converted.
Can I ask for input more than once?
Yes. Call input() again each time you need another value.
See also
- Python
input()function explained - How to convert user input to numbers in Python
- Python
int()function explained - Python
float()function explained - How to use
tryandexceptblocks in Python - Fix
ValueError: invalid literal for int() with base 10
Next step: learn how to convert input safely and handle bad input without crashing.