To take input in Python, use the built-in input() function: name = input(“Enter your name: “). It pauses the program, shows the prompt, waits for the user to type and press Enter, and returns what they typed as a string. To get a number, convert the result with int() or float().
What Is the input() Function in Python?
input() is a built-in function that reads one line of text from the keyboard (standard input). Its syntax is input(prompt), where the optional prompt is a message shown to the user.
- It always returns a str, even if the user types 42.
- The trailing Enter key (newline) is removed from the result.
- The program is paused until the user presses Enter.
How to Take User Input in Python (Strings)
Because input() already returns a string, taking string input needs no conversion:
name = input(“Enter your name: “)
print(f”Welcome, {name}!”)
# Run:
# Enter your name: Rahul
# Welcome, Rahul!
To “give” input, run the script in a terminal or IDE, type your value after the prompt, and press Enter. In Jupyter, a text box appears below the cell.
How to Take Integer Input in Python
Wrap input() in int() for whole numbers or float() for decimals:
age = int(input(“Enter your age: “))
price = float(input(“Enter price: “))
print(age + 1, price * 2)
If the user types something that is not a number, int() raises ValueError. Handle it with try/except (see the Exception Handling topic).
How to Take List Input in Python
The most common method: read the whole line, split() it into parts, and convert each part:
# User types: 10 20 30
nums = list(map(int, input(“Enter numbers: “).split()))
print(nums) # [10, 20, 30]
# Comma-separated: apple,banana,mango
fruits = input(“Enter fruits: “).split(“,”)
print(fruits) # [‘apple’, ‘banana’, ‘mango’]
Alternatively, ask for the count first and read one item per line with a loop:
n = int(input(“How many items? “))
items = []
for i in range(n):
items.append(input(f”Item {i+1}: “))
print(items)
How to Build a Simple Calculator Using input() in Python
Part 2 — Tutorial
In this tutorial you will build a calculator that reads two numbers and an operator from the user with input(), converts them, and prints the result. It takes about 15 minutes and 5 steps and uses input(), float(), if/elif and print().
Prerequisites
- Python 3 installed and able to run a script from the terminal.
- Basic understanding of variables and print().
Steps
Step 1: Read the first number
Convert the text to float so decimals work.
a = float(input(“First number: “))
Step 2: Read the operator as a string
No conversion is needed; strip() removes accidental spaces.
op = input(“Operator (+ – * /): “).strip()
Step 3: Read the second number
Same conversion as step 1.
b = float(input(“Second number: “))
Step 4: Calculate with if/elif
Check the operator and guard against division by zero.
if op == “+”:
result = a + b
elif op == “-“:
result = a – b
elif op == “*”:
result = a * b
elif op == “/” and b != 0:
result = a / b
else:
result = “Invalid input”
Step 5: Print the result
Display it with an f-string, then run the script and test it with different inputs.
print(f”Result: {result}”)