Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › input()

Python Reference

input()

Read user input from the console. Always returns a string.

Last updated: October 2026

Definition: input(prompt) reads a line from standard input and returns it as a string. It optionally prints a prompt first. The trailing newline is stripped.

🎬 Live Example

Five common input patterns. Click any button to see the output.

Input:

                
Output:

            

💡 input() always returns str. Convert types explicitly.

📝

Basic Syntax

input(prompt)

# prompt — optional string shown before reading
# Returns: str (always)
# Strips the trailing newline
# Raises EOFError on end-of-file

🎯 Parameters

Parameter Type Description
promptstrOptional. Shown before reading. No newline added.
1

Basic Input

name = input()
# User types: Alice
# name = 'Alice'

# Always a string
age = input()
# User types: 30
# age = '30' (str, not int!)
2

With Prompt

name = input("Enter your name: ")
# Displays: Enter your name:
# Then waits for input

age = input("Age: ")
# Displays: Age: 

Pro tip: The prompt is printed to stdout without a newline, so the cursor stays on the same line.

3

Convert to Integer or Float

# ✅ Convert immediately
age = int(input("Age: "))

price = float(input("Price: "))

# ❌ This compares a string to an int — always False
age = input("Age: ")
if age > 18:  # TypeError in Python 3!
    ...

Common bug: Forgetting to convert. input() always returns a string, so comparisons with numbers fail.

4

Read Multiple Values

# User types: 10 20
a, b = input().split()
# a='10', b='20' (both str)

# Convert to int
a, b = map(int, input().split())
# a=10, b=20

# Read a list of numbers
nums = list(map(int, input().split()))
5

Real-World: Safe Input Loop

while True:
    try:
        age = int(input("Enter age: "))
        break
    except ValueError:
        print("Please enter a valid number.")

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

# Handles non-numeric input gracefully

🎯 input vs sys.stdin vs file reading

Method Use when Notes
input()Interactive promptsPrints prompt, strips newline
sys.stdin.readline()No prompt neededKeeps newline
open().read()Reading from filesNot user input

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does input() do in Python?

input() reads a line of text from the user and returns it as a string. It optionally prints a prompt first. The trailing newline is stripped.

How do I get an integer from input()?

input() always returns a string. Convert it: int(input('Enter a number: ')). Wrap in try/except to handle invalid input.

What does input() return on EOF?

input() raises EOFError if the user sends end-of-file (Ctrl+D on Unix, Ctrl+Z on Windows). Handle it with try/except for scripts reading from pipes.

Does input() include the newline?

No. input() strips the trailing newline character before returning the string.

How do I read multiple values on one line?

Use input().split(): a, b = input().split(). For integers: a, b = map(int, input().split()).

🎯 What's Next?

← All Python Reference