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()withsepandend - 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):
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):
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}")input() is always a string
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:
| Specifier | Effect | Example |
|---|---|---|
:.2f | 2 decimal places | 3.14159 → 3.14 |
:, | thousands separators | 1000000 → 1,000,000 |
:05d | pad with zeros to width 5 | 42 → 00042 |
:.0% | as a percentage | 0.25 → 25% |
:>10 | right-align in 10 chars | 'hi' → ' hi' |
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 spacesComments, 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:
Variables & functions
snake_case
Constants
UPPER_SNAKE_CASE
Classes
PascalCase
Indentation
4 spaces
"""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
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.