-
Hajipur, Bihar, 844101
User input allows your program to interact with the person who is using it. Instead of writing fixed values in your code, you can ask the user to type something and use that value during the program’s execution. Python provides an easy way to accept input using the input() function.
In this tutorial, you will learn how input() works, how to convert user input into numbers, how to handle errors, how to ask multiple questions, and how to build simple interactive programs.
User input refers to the data entered by someone who is running your program. When the program pauses and waits for information, it becomes interactive.
For example:
Asking the user’s name
Getting age
Asking for price or quantity
Taking login details
Entering marks or scores
Almost every application collects input, and Python makes this process simple and flexible.
The input() function pauses the program and waits for the user to type something. Whatever the user enters is returned as a string.
Example:
name = input("Enter your name: ")
print("Hello", name)
When the user types “Aarohi”, the output becomes:
Hello Aarohi
No matter what the user types, Python treats it as a string by default.
Example:
age = input("Enter your age: ")
print(type(age))
Even if the user enters 20, the type will be str.
To use input as a number, you must convert it using int() or float().
age = int(input("Enter your age: "))
print(age + 1)
price = float(input("Enter the price: "))
print(price + 5.5)
If the user types something that is not a number, Python will show an error. This is where validation becomes important.
You can ask several questions one by one.
Example:
name = input("Enter your name: ")
city = input("Enter your city: ")
print(f"{name} lives in {city}.")
You can also ask two values in a single line:
a, b = input("Enter two numbers: ").split()
print(a, b)
If you want them as numbers:
a, b = map(int, input("Enter two numbers: ").split())
Interactive menus make programs feel more user-friendly.
Example:
print("1. Add")
print("2. Subtract")
choice = input("Enter your choice: ")
if choice == "1":
print("You selected Add")
elif choice == "2":
print("You selected Subtract")
else:
print("Invalid choice")
This is common in small applications, tools and console programs.
You can check if the input is correct before using it.
Example: Checking numbers
value = input("Enter a number: ")
if value.isdigit():
print("Valid number")
else:
print("Not a number")
Example: Checking email format
email = input("Enter email: ")
if "@" in email and "." in email:
print("Valid email")
else:
print("Invalid email")
Sometimes users enter incorrect values. Try/except helps avoid program crashes.
Example:
try:
marks = int(input("Enter marks: "))
print("You entered", marks)
except:
print("Please enter a valid number.")
This makes your program more reliable.
name = input("Enter item name: ")
price = float(input("Enter price: "))
qty = int(input("Enter quantity: "))
total = price * qty
print(f"Item: {name}")
print(f"Total: ₹{total}")
This is how shops and small tools take input from users.
username = input("Enter username: ")
password = input("Enter password: ")
print(f"Welcome {username}")
Simple applications often start with a login prompt like this.
Getting a name
input("Enter your name: ")
Reading an integer
age = int(input("Age: "))
Reading a float
num = float(input("Enter temperature: "))
Checking numeric input
x = input("Enter number: ")
x.isdigit()
Getting two inputs
a, b = input("Enter two values: ").split()
Converting two inputs to numbers
a, b = map(int, input("Enter numbers: ").split())
Menu choice
choice = input("Select an option: ")
Using try/except
try:
n = int(input())
except:
print("Invalid")
Inline calculation
value = int(input("Enter amount: "))
print(value * 2)
Input with default style
msg = input("Say something: ")
Python’s input() function allows you to create interactive programs by accepting values directly from the user. All input is treated as a string, but you can convert it into integers or floats when needed. You also learned how to validate input, catch errors, create menus and build small interactive tools. User input is essential for writing programs that respond to real users instead of using fixed values.
Q1. Write a Python program to ask the user to enter their first and last name, then print the full name.
Q2. Write a Python program to take input of two numbers and print their product.
Q3. Write a Python program to ask for temperature in Celsius and convert it to Fahrenheit.
Q4. Write a Python program to ask the user for their favorite color and display it in a sentence.
Q5. Write a Python program to input a number and check if it is even or odd.
Q6. Write a Python program to ask the user to enter their age, convert it to an integer, and check if they are 18 or older.
Q7. Write a Python program to take 3 float inputs and print their average.
Q8. Write a Python program to input a name and print its length using len().
Q9. Write a Python program to read two numbers from the user and print the larger one.
Q10. Write a Python program to input a string and print it in uppercase.