ZetCode

Python input Function

Last modified April 11, 2025

This comprehensive guide explores Python's input function, which reads user input from the console. We'll cover basic usage, type conversion, input validation, and practical examples of interactive programs.

Basic Definitions

The input function reads a line from standard input and returns it as a string (without a trailing newline). It can display an optional prompt message to guide the user.

Key characteristics: always returns a string, blocks program execution until input is received, and raises EOFError if input is terminated (Ctrl+D/Z).

Basic Input Usage

Here's simple usage showing how to get user input with and without a prompt message. The input is always returned as a string.

basic_input.py
# Simple input without prompt
name = input()
print(f"Hello, {name}!")

# Input with prompt
age = input("Enter your age: ")
print(f"You are {age} years old.")

This example demonstrates the two main ways to use input. The first reads input without any prompt, while the second displays a message.

Remember that even if the user enters numbers, input always returns them as strings. You'll need to convert them for numeric operations.

Type Conversion

Since input returns strings, we often need to convert them to other types. This example shows safe conversion techniques.

conversion.py
# Convert to integer
try:
    num = int(input("Enter a number: "))
    print(f"Double: {num * 2}")
except ValueError:
    print("Please enter a valid integer!")

# Convert to float
try:
    price = float(input("Enter price: "))
    print(f"Total with tax: {price * 1.2:.2f}")
except ValueError:
    print("Please enter a valid number!")

These examples demonstrate safe type conversion using try-except blocks. This prevents crashes when users enter invalid data that can't be converted.

The float example also shows formatted output with 2 decimal places for currency values.

Input Validation

For robust programs, we often need to validate input and ask again if invalid. This example shows a pattern for continuous validation.

validation.py
while True:
    age_input = input("Enter your age (1-120): ")
    if age_input.isdigit():
        age = int(age_input)
        if 1 <= age <= 120:
            break
    print("Invalid age. Please try again.")

print(f"Your age is: {age}")

This code repeatedly asks for input until a valid age between 1 and 120 is provided. It first checks if the input consists only of digits.

The validation combines multiple checks: digit check, range check, and conversion safety. This creates a robust input mechanism.

Multiple Inputs

We can process multiple inputs at once by splitting the input string. This example shows how to handle space-separated values.

multiple_inputs.py
# Get multiple numbers on one line
nums_input = input("Enter 3 numbers separated by spaces: ")
nums = nums_input.split()

if len(nums) == 3:
    try:
        a, b, c = map(float, nums)
        print(f"Sum: {a + b + c}")
    except ValueError:
        print("Please enter valid numbers!")
else:
    print("Please enter exactly 3 numbers.")

This example accepts three numbers in one input. The split method divides the string by whitespace, and map converts all values.

The code includes validation for both the number of inputs and their convertibility to numbers, making it robust against various errors.

Password Input

For sensitive input like passwords, we can hide the typing using the getpass module, which works similarly to input.

password.py
from getpass import getpass

username = input("Username: ")
password = getpass("Password: ")

if username == "admin" and password == "secret":
    print("Access granted")
else:
    print("Access denied")

The getpass function hides user input, which is essential for password entry. It's not visible in the console as it's typed.

Note that in some IDEs, getpass might not work as expected and may fall back to regular input. It works best in terminal environments.

Best Practices

Source References

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.