Phase 1 · FoundationsModule 4~28 min read

Input, Output & Program Structure

Talk to the user with input() and print(), and structure clean, readable scripts.

What you'll learn

Programs need to communicate — to show results and to accept what the user types. This module goes deeper on print() and input(), shows how to format output beautifully, and covers the habits that make Python code clean and readable.

By the end you'll be able to:

  • Control print() with sep and end
  • Read user input with input() and convert it
  • Format numbers and align output with f-string specifiers
  • Write comments, docstrings, and PEP 8-compliant code

Output with print()

You know print() displays text. It has two handy options: sep controls what goes between multiple values, and end controls what goes at the end (it defaults to a newline — set it to "" or a space to keep printing on the same line):

output.py
print("a", "b", "c")              # a b c   (space is the default sep)
print("a", "b", "c", sep="-")     # a-b-c
print("no newline...", end=" ")   # end replaces the newline
print("same line")

Reading input

The input() function pauses the program, waits for the user to type a line, and returns it. One crucial detail: it always returns a string — so to do maths, you must convert it with int() or float() (Module 2):

greet.py
name = input("What's your name? ")
age = int(input("Your age? "))    # input() gives a str — convert it!

print(f"Hi {name}, next year you'll be {age + 1}")
Press Run to see a sample interaction.

input() is always a string

Forgetting to convert is the #1 beginner bug here. input() gives you "20" (text), not 20 (a number) — so "20" + 1 is an error. Wrap it in int(...) when you need a number.

Formatting output

f-strings can do much more than insert values — after a colon, a format specifier controls exactly how a value looks: decimal places, padding, alignment, percentages, and more:

SpecifierEffectExample
:.2f2 decimal places3.14159 → 3.14
:,thousands separators1000000 → 1,000,000
:05dpad with zeros to width 542 → 00042
:.0%as a percentage0.25 → 25%
:>10right-align in 10 chars'hi' → ' hi'
format.py
price = 1234.5678

print(f"{price:.2f}")     # 1234.57    two decimals
print(f"{price:,.2f}")    # 1,234.57   thousands separator
print(f"{42:05d}")        # 00042      pad with zeros
print(f"{0.25:.0%}")      # 25%        percentage
print(f"{'hi':>8}")       # right-align in 8 spaces

Comments, docstrings & style

Readable code is a core Python value. Use # comments to explain the why, and docstrings (triple-quoted strings) to document modules, functions, and classes. Just as important is following PEP 8, Python's official style guide — especially its naming conventions:

PEP 8 naming at a glance

Variables & functions

snake_case

total_priceget_data()

Constants

UPPER_SNAKE_CASE

MAX_SIZEPI

Classes

PascalCase

BankAccount

Indentation

4 spaces

never tabs
shapes.py
"""Area calculations for shapes."""   # a module docstring

PI = 3.14159   # constants use UPPER_CASE by convention

def circle_area(radius):
    """Return the area of a circle with the given radius."""
    return PI * radius ** 2

print(circle_area(2))

Tip

Tools do the tedious part for you: black auto-formats your code to PEP 8, and ruff flags style and bug issues. Most editors run them on save. You'll set these up properly in Module 40.

Recap & quick check

Key takeaways

  • print() accepts sep (between values) and end (what finishes the line, default a newline).
  • input() pauses for the user and ALWAYS returns a string — convert with int()/float() for maths.
  • f-string format specifiers after a colon control decimals (:.2f), separators (:,), padding, and alignment.
  • Comment the 'why'; document with docstrings; follow PEP 8 naming (snake_case, UPPER_CASE, PascalCase).
  • Use black to auto-format and ruff to lint — let tools handle style.

Quick check

1. What type does input() return?

2. What does the end argument of print() control?

3. Which f-string specifier shows a number with two decimals?

4. How should you name a variable in Python (PEP 8)?

5. What is a docstring for?

Great — your programs can talk to users and present results cleanly. Next up: Module 5 — Control Flow, where your code starts making decisions.