Python Tutorial: Print & Formatting
Print things properly. f-strings, format(), padding, precision — control every detail of your output.
Make your output readable
`print()` is simple. But real programs need more than just dumping raw values — you need to format numbers, align columns, build readable messages.
Python's f-strings make formatting as easy as writing plain English. Twelve steps.
What you'll learn in this Python print & formatting tutorial
This interactive Python tutorial has 12 hands-on exercises. Estimated time: 18 minutes.
- There's a better way to print — In Chapter 2, when you wanted to print a variable alongside text, you had to do something clunky like `"Hello, " + name …
- Expressions inside f-strings — Here is where f-strings get really powerful. You can put any Python expression inside the `{}` — not just variable name…
- Fix messy decimal places — Floats can be unpredictable: `99.9` prints as `99.9`, but after some math you might get `99.99999999` or `100.00000001`.
- Thousands separators — Reading `8000000000` makes your brain work too hard. `8,000,000,000` is instant.
- Column alignment — For neatly aligned columns, you can set a minimum width and alignment direction:
- print() has hidden powers — `print()` has two optional superpowers that most beginners don't know about:
- Escape sequences — special characters — Some characters need special treatment inside strings:
- Raw strings — when backslashes get annoying — File paths on Windows are a nightmare: `C:\Users\Alex` — all those backslashes need to be doubled.
- Multi-line strings — Triple quotes `"""` let you write text that spans multiple lines without `\n` everywhere.
- .format() — the older style — Before f-strings existed (they were added in Python 3.6), the standard was `.format()`:
- % formatting — the ancient style — Before `.format()` there was `%` formatting — Python's original approach:
- Build a receipt — Final challenge — let's build something real.
Python Print & Formatting concepts covered
- Make your output readable