0Pricing
Python Academy · Lesson

String Formatting with f-strings

Use f-strings and format() for clean, readable output.

String Formatting with f-strings is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

F-strings (f'...') are Python's modern string interpolation syntax — fast, readable, and powerful.

Basic f-string

Prefix a string with f and put expressions in {}. f'Hello {name}' inserts the value of name inline.
name = 'Alice'
print(f'Hello {name}!')

Expressions in f-strings

Any Python expression works inside {}: f'{2 + 2}' gives '4'. f'{len(name)}' inserts the length.
x = 10
print(f'Square: {x**2}')

Format Specifiers

f'{3.14159:.2f}' rounds to 2 decimal places. f'{1000000:,}' adds comma separators.
pi = 3.14159
print(f'{pi:.2f}')
print(f'{1000000:,}')

Width and Alignment

f'{name:<20}' left-aligns in 20 chars. f'{name:>20}' right-aligns. f'{name:^20}' centers.
name = 'Alice'
print(f'{name:^20}')

Debug with =

f'{x=}' prints the expression and its value: 'x=42'. Perfect for quick debugging.
x = 42
print(f'{x=}')

The format() Method

'Hello {}'.format('World') works like f-strings but can be stored as templates. '{0} {1}'.format('a','b') uses positional args.
print('Hello {}'.format('World'))

The % Operator (Legacy)

'Hello %s, you are %d' % ('Alice', 30) is the old C-style formatting. Avoid in new code, but recognize it in legacy projects.
print('Hello %s, age %d' % ('Bob', 25))

Multi-line f-strings

Combine triple quotes and f: f'''Name: {name}\nAge: {age}''' for readable multi-line templates.
name, age = 'Alice', 30
print(f'Name: {name}\nAge: {age}')

Nested Quotes in f-strings

If the f-string uses single quotes, use double inside {}: f'Value: {d["key"]}'. Or use a temp variable to avoid confusion.
d = {'key': 'val'}
print(f'Value: {d["key"]}')

Performance of f-strings

F-strings are faster than .format() and % because they are evaluated at parse time. Prefer them in new code.

Quick Check

What does f'{x=}' print when x = 5?

Recap

F-strings: f'{expr}' for interpolation, f'{x:.2f}' for formatting, f'{x=}' for debug output. Faster and more readable than % or .format().

Keep Going

Great work! Move on to the next lesson to continue building your skills.

Frequently asked questions

Is the “String Formatting with f-strings” lesson free?

Yes — the full text of “String Formatting with f-strings” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “String Formatting with f-strings”?

Use f-strings and format() for clean, readable output. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “String Formatting with f-strings” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. String Basics and Indexing
  2. String Slicing and Concatenation
  3. Common String Methods
  4. String Formatting with f-strings
← Back to Python Academy