PythonMastery

Take user input in Python

Ask the user for their name with input() and greet them. Also shows how to turn typed text into a number with int() or float().

input() shows a message, waits for the user to type something and press Enter, and gives you back what they typed.

The program

python
name = input("Enter your name: ")
print(f"Hello, {name}!")

Typed in this example: Ada. Press Run to type your own.

output
Enter your name: Ada
Hello, Ada!

How it works

Numbers from input

Because input() always gives you a string, convert it before you do maths with it. Use int() for whole numbers and float() for numbers with a decimal point:

python
age = int(input("How old are you? "))
print(f"In ten years you will be {age + 10}.")

Typed in this example: 25. Press Run to type your own.

output
How old are you? 25
In ten years you will be 35.

If the user types something that is not a number, such as twenty, int() raises a ValueError. The ValueError page shows how to handle that.

Learn the idea: Input & Output: Talking to Your User