7 min read
🐍beginner

Python Input & Output — Talking to Users

Learn how to get information from users with input() and display results beautifully with print() and f-strings.

Why Input and Output Matter

A program that can't talk to people is pretty useless! Input and output (often called I/O) is how your program communicates: • Output = your program talking TO the user (showing results, messages, errors) • Input = the user talking TO your program (typing answers, entering data) Every app you use does this: games show your score (output) and accept button presses (input). Calculators show answers (output) and accept numbers (input). Let's learn how Python does it!

Print — Output to the Screen

python
# Basic print
print("Hello, World!")

# Print multiple things
name = "Aarav"
age = 12
print("Name:", name, "Age:", age)
# Output: Name: Aarav Age: 12

# f-strings — the BEST way to format output
print(f"Hi, I'm {name} and I'm {age} years old!")

# You can put any expression inside {}
print(f"Next year I'll be {age + 1}")
print(f"My name has {len(name)} letters")

# Special characters
print("Line 1\nLine 2")  # \n = new line
print("Price:\t$9.99")    # \t = tab

Input — Getting Info from Users

python
# input() pauses and waits for the user to type
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")

# IMPORTANT: input() always returns a STRING!
age_text = input("How old are you? ")
print(type(age_text))  # <class 'str'>

# Convert to number for math
age = int(input("How old are you? "))
next_year = age + 1
print(f"Next year you'll be {next_year}!")

# For decimal numbers, use float()
height = float(input("Your height in feet: "))
print(f"That's {height * 30.48:.1f} centimeters!")

The #1 Beginner Mistake with input()

This trips up almost everyone: input() ALWAYS gives you a string, even if the user types a number. If someone types 25, you get the string "25" not the number 25. If you try to do math with it, you'll get weird results: • "25" + "10" = "2510" (it glued the strings together!) • int("25") + int("10") = 35 (now it does real math) Always convert with int() or float() when you need to do math with user input.

Formatting Numbers Nicely

python
price = 1234.5678

# Round to 2 decimal places
print(f"Price: ${price:.2f}")  # Price: $1234.57

# Add commas for big numbers
big = 1000000
print(f"Population: {big:,}")  # Population: 1,000,000

# Percentage
score = 17
total = 20
percent = (score / total) * 100
print(f"You got {score}/{total} = {percent:.1f}%")
# You got 17/20 = 85.0%

# Padding (useful for tables)
for item, cost in [("Apple", 1.5), ("Banana", 0.75), ("Mango", 3.0)]:
    print(f"{item:<10} ${cost:.2f}")
# Apple      $1.50
# Banana     $0.75
# Mango      $3.00
Pro Tip

Always put a space at the end of your input() prompt — like input("Your name: ") instead of input("Your name:"). That little space makes the user's typing not squish up against your question. Small detail, big difference in how polished your program feels!

Try It Yourself

Build a mini receipt printer! Ask the user for 3 items and their prices. Calculate the subtotal, add 8% tax, and print a nicely formatted receipt with the items lined up, the subtotal, tax amount, and grand total — all with proper dollar formatting like $12.50.

Ready to build?

Put what you learned into practice — pick a project and start coding.

Start Building Free