Reading User Input
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Interactive programs need to accept data from the user. Python provides the built-in input() function for reading lines from the terminal.
The input() function
input() is a built-in function that shows a prompt, waits for the user to type a line and press Enter, then returns that line as a string.
A program often needs a value from the person using it. input() shows a prompt, waits for a line, and hands the typed text back.
user_name = input("Enter your name: ")print(f"Hello, {user_name}!")Example 1 · Greeting a customer
name = input("Customer name: ")print(f"Welcome, {name}!")Customer name: Asha Welcome, Asha!
What this code does
- The prompt is printed without a new line, so the answer appears right after it.
- The typed text goes into
nameand is used in the f-string.
The typed text is stored in user_name. End the prompt with ": " so the answer does not touch it.
input() always returns a string
Even when the user types 25, input() returns the text "25". Text and numbers behave differently, so convert with int() or float() before calculating.
age = int(input("Age: "))print(f"Next year you will be {age + 1}")Example 2 · Next year's age
age = int(input("Age: "))print(f"Next year: {age + 1}")print(type(age))Age: 29 Next year: 30 <class 'int'>
What this code does
input()gives"29";int()turns it into the number 29.type(age)confirms it is an int, soage + 1works.
Without int(), age + 1 would raise a TypeError. If the text is not a number, such as abc, int() raises a ValueError; later lessons show how to handle that.
Cleaning input with strip()
strip() is a string method that returns a copy of the text without whitespace at the start and the end.
People often type a space before or after their answer. .strip() removes spaces from both ends, and you can chain it straight onto input().
city = input("City: ").strip()print(f"City: [{city}]")Example 3 · Spaces around an answer
city = input("City: ").strip()print(f"[{city}]")City: Mumbai [Mumbai]
What this code does
- The user typed two spaces before the city.
.strip()removes them, so the brackets touch the text.
The square brackets show that no spaces are left around Mumbai. int() also ignores spaces around digits, but a name does not.
Reading more than one value
A form needs several answers. Call input() once per value and convert each one to the type it needs.
name = input("Product: ")qty = int(input("Quantity: "))price = float(input("Price: "))print(f"{name}: Rs. {qty * price:,.2f}")Example 4 · Order total from three answers
name = input("Product: ")qty = int(input("Quantity: "))price = float(input("Price: "))print(f"{name} x {qty}")print(f"Total: Rs. {qty * price:,.2f}")Product: Keyboard Quantity: 3 Price: 1299.50 Keyboard x 3 Total: Rs. 3,898.50
What this code does
- Each value is converted to the type it needs: text, int and float.
- The total uses the comma and two-decimal specifiers from the last lesson.
Each call waits for its own line. The number of input() calls must match the lines the user types.
Next step
Check your understanding
9 quick questions