f-string Expressions
Embed expressions in strings.
f-string Expressions is a free Python Academy lesson on CoddyKit — lesson 1 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.
What is an f-string?
An f-string is a string prefixed with f that lets you embed Python expressions directly inside braces {}. Introduced in Python 3.6, they are the clearest way to build strings.
name = 'Ada'
print(f'Hello, {name}!')Embedding variables
Any variable name placed in {} is replaced by its value, converted to a string automatically.
age = 30
city = 'Istanbul'
print(f'I am {age} and live in {city}.')Inline arithmetic
You can put full expressions inside the braces, not just variable names. The expression is evaluated and its result inserted.
a = 6
b = 7
print(f'{a} times {b} is {a * b}')Calling functions
Function and method calls work inside f-strings too, so you can transform values on the fly.
word = 'python'
print(f'Upper: {word.upper()}')
print(f'Length: {len(word)}')Conditional expressions
A ternary expression fits neatly inside braces, letting you choose text based on a condition.
score = 72
print(f'You {"passed" if score >= 60 else "failed"} the exam.')Accessing dict and list items
Indexing and key lookups work inside f-strings. Use a different quote style for string keys to avoid clashing with the outer quotes.
user = {'name': 'Lin', 'pts': 90}
nums = [10, 20, 30]
print(f"{user['name']} scored {user['pts']}")
print(f'First number: {nums[0]}')Accessing attributes
You can reach into object attributes with dot notation right inside the braces.
import math
print(f'Pi is roughly {math.pi}')
print(f'E is roughly {math.e}')The self-documenting equals sign
Add = after an expression to print both the expression text and its value. This is a great debugging shortcut introduced in Python 3.8.
x = 5
y = 12
print(f'{x + y = }')Escaping braces
To include literal curly braces in the output, double them: {{ and }}.
value = 42
print(f'A set looks like {{{value}}}')Multiline f-strings
Triple-quoted f-strings span multiple lines while still evaluating every embedded expression.
name = 'Sam'
role = 'admin'
report = f'''User: {name}
Role: {role}
Active: {True}'''
print(report)Nesting f-strings
You can even nest expressions and call other f-string-producing code, building strings from smaller pieces.
items = ['a', 'b', 'c']
print(f'Items: {", ".join(items)}')Quick Check
What will f'{2 + 3}' produce?
Recap: f-string Expressions
You learned that f-strings:
- Are prefixed with
fand embed expressions in{}. - Evaluate arithmetic, function calls, indexing, attributes, and conditionals.
- Support
{expr = }for debugging output. - Use
{{and}}for literal braces and can span multiple lines.
name = 'World'
print(f'Hello {name}, 2+2={2+2}')Frequently asked questions
Is the “f-string Expressions” lesson free?
Yes — the full text of “f-string Expressions” 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 “f-string Expressions”?
Embed expressions in strings. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “f-string Expressions” 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
- f-string Expressions
- Format Specifiers
- Format Mini-Language
- Template Strings and str.format