Home › Python with Chai › Lesson 14
f-strings: a neat bill
Python with Chai, lesson 14 of 20. A short Hinglish video, simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
"Total: Rs " + total crashes with a TypeError: Python won't add text and a number.
An f-string has an f before the quotes. Anything inside { } is filled in with its value, no str() needed.
You can even do maths inside the braces: {total + 10}.
The code
total = 50
cups = 3
print(f"{cups} cup chai: Rs {total}")
print(f"Tip ke saath: Rs {total + 10}")
Output:
3 cup chai: Rs 50 Tip ke saath: Rs 60
Try it yourself
- Add a variable flavour = "Masala" and print: Masala chai x 3 = Rs 50
- Remove the f and run it again. What gets printed?
Remember
Put an f before the quotes, then fill in the { }.
← Modules: the lucky chai · range(): 10 cups at once →
All lessons: Python with Chai