pi = 3.14159265
print(f"Pi rounded to 2 decimals: {pi:.2f}")
# Output: Pi rounded to 2 decimals: 3.14
4. Date formatting
from datetime import datetime
today = datetime.now()
print(f"Today is {today:%d-%m-%Y}")
# Output: Today is 17-09-2025 (example)
5. Debugging (Python 3.8+)
You can append = inside {} to print both the expression and its value:
x = 5
print(f"{x=}")
# Output: x=5
Performance
F-strings are faster than .format() and % formatting because they are evaluated at
runtime and don’t need extra function calls.
Example:
name = "Anna"
age = 25
# Old style
print("My name is %s and I am %d" % (name, age))
# str.format()
print("My name is {} and I am {}".format(name, age))
# F-string
print(f"My name is {name} and I am {age}")
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
What are F-Strings?
{}.forF.Example:
Why Use F-Strings?
.format()or%formatting.{}.Example:
Features of F-Strings
1. Expressions inside {}
2. Function calls inside {}
3. Formatting numbers
4. Date formatting
5. Debugging (Python 3.8+)
You can append
=inside{}to print both the expression and its value:Performance
Example:
Read More -