capypad
0 day streak
python / beginner
Snippet

Modern String Formatting (f-strings)

F-strings provide a concise and readable way to include expressions or variables inside string literals by prefixing the string with 'f'.

snippet.py
python
1
2
3
4
5
6
name = "Alice"
age = 25
 
# Using f-strings to embed variables
message = f"Hello, {name}! You are {age} years old."
print(message)
Breakdown
1
name = "Alice"
Assigns a string value to a variable.
2
f"... {name} ..."
The 'f' prefix allows Python to replace {name} with its actual value.