Python User Input Tutorial


User input is a fundamental aspect of interactive programs, allowing users to provide data to the program at runtime. Python provides simple ways to handle user input using the input() function. This tutorial covers the basics of obtaining and processing user input in Python.

Table of Contents

  1. Getting User Input
  2. Converting Input Types
  3. Handling Errors
  4. Multi-line Input
  5. Practical Examples
  6. Conclusion

1. Getting User Input

The input() function is used to get input from the user. It reads a line from the input (usually from the user), converts it to a string, and returns it.


# Getting user input
name = input("Enter your name: ")
print("Hello, " + name + "!")


Enter your name: Alice
Hello, Alice!

2. Converting Input Types

The input() function always returns the input as a string. To work with other data types, you need to convert the input.


# Getting integer input
age = input("Enter your age: ")
age = int(age)
print("You are " + str(age) + " years old.")

# Getting float input
height = input("Enter your height in meters: ")
height = float(height)
print("Your height is " + str(height) + " meters.")


Enter your age: 25
You are 25 years old.
Enter your height in meters: 1.75
Your height is 1.75 meters.

3. Handling Errors

When converting user input, errors can occur if the input is not in the expected format. You can handle these errors using try-except blocks.


# Handling errors
    age = int(input("Enter your age: "))
    print("You are " + str(age) + " years old.")
except ValueError:
    print("Invalid input. Please enter a valid number.")


Enter your age: twenty
Invalid input. Please enter a valid number.

4. Multi-line Input

To get multi-line input from the user, you can use a loop or the input() function repeatedly.

Example using a Loop

# Multi-line input using a loop
print("Enter your favorite fruits (type 'done' to finish):")
fruits = []
while True:
    fruit = input()
    if fruit.lower() == 'done':

print("Your favorite fruits are:", fruits)


Enter your favorite fruits (type 'done' to finish):
Your favorite fruits are: ['Apple', 'Banana', 'Cherry']

Example using

Another method for multi-line input is using, which reads input until EOF.

import sys

# Multi-line input using
print("Enter your address (press Ctrl+D to finish):")
address =
print("Your address is:")

5. Practical Examples

Example 1: Simple Calculator

# Simple calculator
    num1 = float(input("Enter first number: "))
    num2 = float(input("Enter second number: "))
    operation = input("Enter operation (+, -, *, /): ")

    if operation == '+':
        result = num1 + num2
    elif operation == '-':
        result = num1 - num2
    elif operation == '*':
        result = num1 * num2
    elif operation == '/':
        result = num1 / num2
        print("Invalid operation")
        result = None

    if result is not None:
        print("Result:", result)
except ValueError:
    print("Invalid input. Please enter valid numbers.")

Example 2: Greeting Program

# Greeting program
name = input("Enter your name: ")
age = input("Enter your age: ")

    age = int(age)
    print(f"Hello, {name}! You are {age} years old.")
except ValueError:
    print("Invalid age. Please enter a valid number.")

Example 3: Collecting User Preferences

# Collecting user preferences
print("Please enter your top 3 favorite movies:")
movies = []
for i in range(3):
    movie = input(f"Movie {i + 1}: ")

print("Your favorite movies are:", movies)

6. Conclusion

Handling user input is a crucial aspect of making interactive programs. Python's input() function makes it easy to get user input, and by converting input types and handling errors, you can create robust programs that can interact with users effectively. This tutorial covered the basics of getting user input, converting input types, handling errors, working with multi-line input, and provided practical examples to illustrate these concepts.