Skip to content

F-string

In one line

An f-string is a string prefixed with f that embeds expressions in curly braces for inline formatting.

In simple words

An f-string (formatted string literal) is a string prefixed with f or F that lets you embed Python expressions directly inside the string using curly braces {}.

F-strings were introduced in Python 3.6 and are the recommended way to format strings. They are faster and more readable than % formatting or .format().

You can put any expression inside the braces: variables, arithmetic, function calls, even attribute access. Format specifiers go after a colon: f"{pi:.2f}" formats pi to 2 decimal places. Debugging is easier with f"{x=}" (Python 3.8+) which prints the variable name and value.

Example

python
name = "Alice"
age = 30
pi = 3.14159

# Basic f-string
print(f"Hello, {name}!")  # Hello, Alice!

# Expressions inside braces
print(f"{name} is {age} years old")  # Alice is 30 years old
print(f"Next year: {age + 1}")  # Next year: 31

# Format specifiers
print(f"Pi: {pi:.2f}")  # Pi: 3.14
print(f"Age: {age:>5}")  # Age:    30

# Debug format (Python 3.8+)
print(f"{age=}")  # age=30

F-strings embed expressions in {} for inline formatting. They support format specifiers after a colon.

Common confusions

  • Confused with: format method

    The difference: f-strings (f'...') embed expressions directly in the string. .format() uses placeholders: '{}'.format(value). f-strings are faster, more readable, and the modern recommendation. Use f-strings for new code.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary