Puts values into text.
Synopsis
f"text {value} more text"
f"{number:.2f}"
f"{number:,}"
Description
Put f before the opening quote. Python replaces each { } with the value of what is inside, converting it to text for you.
After a colon you can say how to show a number: .2f means 2 decimal places, and , adds commas.
Parameters
| Name | Meaning |
|---|---|
{ } |
Any variable or expression, such as {name} or {x + y}. |
Return value
A str.
Example
name = "David"
x = 3
print(f"hello, {name}")
print(f"{x} squared is {x * x}")
print(f"{3.14159:.2f}")
print(f"{1000000:,}")
It prints:
hello, David
3 squared is 9
3.14
1,000,000
Common mistake
name = "David"
print("hello, {name}")
It prints:
hello, {name}
Without the f, the braces are printed as they are.
See also
Last updated 2 weeks ago