What are f-strings in Python?
An f-string (formatted string) is a string that starts with f and can hold variables and expressions inside curly braces { }. They were added in Python 3.6 and are the easiest way to format text.
name = "Robin"
age = 20
price = 49.5
print(f"{name} is {age} years old")
print(f"Next year: {age + 1}")
print(f"Total: {price:.2f}")
Output:
Tutorials Class - Output Window
Robin is 20 years old
Next year: 21
Total: 49.50
:.2f shows the number with two digits after the decimal point. F-strings are shorter and easier to read than the older % formatting or format().
Learn more about the similar topics:
| Tutorials |
|---|
| Python Strings |
| Exercises & Assignments |
|---|
| Python program to check palindrome string |
| Python program to count vowels in a string |
| Python program to reverse a string |
| Interview Questions & Answers |
|---|
| How would you convert a string into lowercase? |
| How would you convert a string into uppercase? |
| Why do we use the join() function in Python? |
| What are f-strings in Python? |