TIL: f-string formatting - a cheat sheet
I am a big fan of f-strings in Python. If you are not using them yet, you should!
f-strings come with a string formatting syntax that makes it very convenient to create nicely formatted strings. For example, you can introduce padding:
>>> v = "test"
>>> f"{v:>20}"
' test'
Or you can format dates and print weekdays instead:
>>> from datetime import datetime
>>> print(f"Happy {datetime.now():%A}!")
Happy Monday!
Or you can round floating point numbers:
>>> print(f"{1.1111:.2f}")
1.11
The trouble is that I ALWAYS forget this syntax and need to search online every single time without good hits.
BUT: this is now solved thanks to this awesome cheatsheet I discovered today: https://fstring.help/cheat/ 🤩
I also recommend reading the underlying article from Python Morsel (which provides awesome weekly Python exercises but that’s a post for another day).