Have Mercy on Yourself And Learn These 9 Everyday Python f-string Codes

The ‘eff’ in f-strings may as well stand for… (not that! What a twisted mind you have) for ‘freaking awesome’!

Bex T.
6 min readMar 14, 2023
Photo by drmakete lab

If you clicked on this article, you've probably already used f-strings in Python. You know that you can throw almost any expression between braces to sprinkle dynamic values into strings.

But, from time to time, you see a guy using something like print(f"{f'${number:.3f}':>10s}") (probably to show off) and you undoubtedly say what the f...-string is that?!

By the end of this post, you will find the answer to that question and acquire the skills to write even more messed up but very useful f-string codes that take your string manipulation skills to a whole new level (and make others wonder if you are just showing off, too).

Basics of format codes

Format codes in f-strings start with an expression inside braces:

from datetime import date

print(f"Today is {date.today()}")
print(f"This is result from a lambda: {(lambda x: x ** 2)(3)}")
Today is 2023-03-13
This is result from a lambda: 9

--

--

Bex T.

BEXGBoost | DataCamp Instructor |🥇Top 10 AI/ML Writer on Medium | Kaggle Master | https://www.linkedin.com/in/bextuychiev/