Python User Input


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.

What Is User Input?

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.

Using the input() Function

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

Input Is Always a String

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.

Converting Input Into Numbers

To use input as a number, you must convert it using int() or float().

Integer input

age = int(input("Enter your age: "))
print(age + 1)

Float input

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.

Asking Multiple Inputs

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())

Creating Simple Menus

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.

Validating User Input

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")

Using Try/Except for Safer Input

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.

Real Life Example: Simple Billing Program

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.

Real Life Example: Login Prompt

username = input("Enter username: ")
password = input("Enter password: ")

print(f"Welcome {username}")

Simple applications often start with a login prompt like this.

Practical Examples

  1. Getting a name

input("Enter your name: ")
  1. Reading an integer

age = int(input("Age: "))
  1. Reading a float

num = float(input("Enter temperature: "))
  1. Checking numeric input

x = input("Enter number: ")
x.isdigit()
  1. Getting two inputs

a, b = input("Enter two values: ").split()
  1. Converting two inputs to numbers

a, b = map(int, input("Enter numbers: ").split())
  1. Menu choice

choice = input("Select an option: ")
  1. Using try/except

try:
    n = int(input())
except:
    print("Invalid")
  1. Inline calculation

value = int(input("Enter amount: "))
print(value * 2)
  1. Input with default style

msg = input("Say something: ")

Summary of the Tutorial

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.


Practice Questions

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.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top