F-strings are a new-ish [[Python]] feature similar to [[JavaScript]]'s new-ish template strings (the strings that use backticks).... but F-strings pack in even more functionality, also the prefixing syntax using an `f` before the string I think is a better signal than the use of slightly different-looking quotes. Like template strings, you can embed arbitrary expressions in the string itself. Unlike JavaScripts template strings, you can use f-strings to do some presentation formatting, like padding & rounding. See below.
```python
# f-string tricks
# escaping curly braces
f"Formatted strings make use of {{ and }} to do cool stuff" # becomes "Formatted strings make use of { and } to do cool stuff"
# Embedding variables
name = "Aaron"
greeting = f"Hello {name}" # becomes "Hello Aaron"
# Arbitrary expressions
a, b = 6, 7
sumString = f"{a} + {b} = {a+b}" # becomes "6 + 7 = 13"
# String methods
f"This is me {'yelling'.upper()}" # becomes "This is me YELLING"
# Number formatting
pi = 3.14159265358979323846264338
f"Pi, according to most, is {pi:.2f}" # becomes "Pi, according to most, is 3.14"
bigNum = 123456
f"{bigNum:,}" # becomes "123,456"
# Padding & Alignment
f"{'Left':<10}" # becomes "Left "
f"{'Right':>10}" # becomes " Right"
f"{'Center':^10}" # becomes " Center "
f"{42:0>5}" # becomes "00042"
f"{42:*^7}" # becomes "**42***"
# Date & Time
from datetime import datetime
now = datetime(2025, 1, 18)
f"{now:%Y-%m-%d}" # becomes "2025-01-18"
```
****
# More
## Source
- Textbook & ChatGPT
## Related