🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Python f-strings — Modern String Formatting Mastery

Python Basicsf-strings🟢 Free Lesson

Learning Objectives

By the end of this tutorial, you will be able to:

  • Understand what f-strings are and why they are the preferred string formatting method in Python
  • Use basic f-string syntax with variables and expressions
  • Apply format specifiers for width, precision, alignment, fill characters, and types
  • Use the debugging = syntax (Python 3.8+) to inspect values
  • Create multiline and nested f-strings
  • Compare f-strings with %-formatting, .format(), and string.Template
  • Identify common f-string mistakes and how to avoid them
  • Apply f-strings in real-world scenarios like logging, SQL queries, and UI formatting

What Are f-strings

f-strings (formatted string literals) were introduced in Python 3.6 (PEP 498). They provide a concise and readable way to embed expressions inside string literals. The f or F prefix tells Python to evaluate the expression inside the braces at runtime and include the result in the string.

python
name = "Alice"
greeting = f"Hello, {name}!"
print(greeting)  # Hello, Alice!

f-strings are faster than both %-formatting and .format() because the expression is evaluated at runtime and the result is directly embedded into the string, avoiding function call overhead.

Basic Syntax

Variable Interpolation

python
first_name = "John"
last_name = "Doe"
age = 30

print(f"Name: {first_name} {last_name}, Age: {age}")
# Output: Name: John Doe, Age: 30

Using Dict and List Access

python
person = {"name": "Sara", "city": "Paris"}
scores = [95, 87, 92]

print(f"City: {person['city']}")   # Output: City: Paris
print(f"First score: {scores[0]}") # Output: First score: 95

Calling Methods Inside f-strings

python
message = "hello world"
print(f"Uppercase: {message.upper()}")  # Output: Uppercase: HELLO WORLD
print(f"Title: {message.title()}")      # Output: Title: Hello World

Using Format Specifiers Inline

python
pi = 3.14159265
print(f"Pi to 2 decimals: {pi:.2f}")  # Output: Pi to 2 decimals: 3.14

Expressions in f-strings

f-strings support any valid Python expression inside the braces.

Arithmetic Expressions

python
x = 10
y = 3

print(f"{x} + {y} = {x + y}")   # Output: 10 + 3 = 13
print(f"{x} * {y} = {x * y}")   # Output: 10 * 3 = 30
print(f"Power: {x ** y}")        # Output: Power: 1000

Conditional Expressions

python
score = 85
print(f"Result: {'Pass' if score >= 60 else 'Fail'}")
# Output: Result: Pass

print(f"Status: {'Excellent' if score >= 90 else 'Good' if score >= 75 else 'Needs improvement'}")
# Output: Status: Good

Calling Functions

python
import math

radius = 5
print(f"Area of circle: {math.pi * radius ** 2:.2f}")  # Output: Area of circle: 78.54

words = ["python", "is", "awesome"]
print(f"Joined: {' '.join(words)}")  # Output: Joined: python is awesome

Using Comprehensions

python
numbers = [1, 2, 3, 4, 5]
print(f"Squares: {[n ** 2 for n in numbers]}")
# Output: Squares: [1, 4, 9, 16, 25]

print(f"Even numbers: {[n for n in numbers if n % 2 == 0]}")
# Output: Even numbers: [2, 4]

Lambda Functions

python
multiply = lambda x, y: x * y
print(f"5 * 3 = {multiply(5, 3)}")
# Output: 5 * 3 = 15

Boolean and None Values

python
is_active = True
print(f"Active: {is_active}")
# Output: Active: True

data = None
print(f"Data: {data}")
# Output: Data: None

print(f"Data: {data!r}")
# Output: Data: None

Format Specifier Mini-Language

The format specifier inside f-strings follows Python's format specification mini-language:

[[fill]align][sign][z][#][0][width][grouping_option][.precision][type]

Width and Alignment

python
name = "Alice"
print(f"|{name:10}|")    # Right-aligned (default): |Alice     |
print(f"|{name:<10}|")   # Left-aligned:            |Alice     |
print(f"|{name:>10}|")   # Right-aligned:           |     Alice|
print(f"|{name:^10}|")   # Center-aligned:          |  Alice   |
print(f"|{name:*^10}|")  # Center with * fill:      |**Alice***|

Precision

python
pi = 3.141592653589793

print(f"2 decimals: {pi:.2f}")   # Output: 2 decimals: 3.14
print(f"4 decimals: {pi:.4f}")   # Output: 4 decimals: 3.1416
print(f"6 decimals: {pi:.6f}")   # Output: 6 decimals: 3.141593

long_string = "This is a very long string"
print(f"Truncated: {long_string:.10}")  # Output: Truncated: This is a

Fill Characters and Sign

python
price = 49.99
print(f"Price: ${price:0>8.2f}")   # Output: Price: $00049.99
print(f"Price: ${price:.<8.2f}")   # Output: Price: $49.99...

positive = 42
negative = -42
print(f"With +: {positive:+5d}, {negative:+5d}")  # Output: With +:   +42,   -42

Numeric Types

python
print(f"Binary: {42:b}")       # Output: Binary: 101010
print(f"Octal: {42:o}")        # Output: Octal: 52
print(f"Hex: {42:x}")          # Output: Hex: 2a
print(f"HEX: {42:X}")          # Output: HEX: 2A
print(f"Scientific: {123456.789:.2e}")  # Output: Scientific: 1.23e+05

ratio = 0.8567
print(f"Percent: {ratio:.1%}")  # Output: Percent: 85.7%

Grouping

python
population = 7_900_000_000
print(f"World population: {population:,}")
# Output: World population: 7,900,000,000

price = 1234567.89
print(f"Price: ${price:,.2f}")
# Output: Price: $1,234,567.89

Format Specifier Reference Table

SpecifierDescriptionExampleOutput
sStringf"{'hello':s}"hello
dDecimal integerf"{42:d}"42
fFixed-point floatf"{3.14:f}"3.140000
eScientific notationf"{3.14:e}"3.140000e+00
gGeneral formatf"{3.14:g}"3.14
%Percentagef"{0.85:%}"85.000000%
bBinaryf"{42:b}"101010
oOctalf"{42:o}"52
xHex lowercasef"{255:x}"ff
XHex uppercasef"{255:X}"FF
cCharacterf"{65:c}"A
nNumber with localef"{1000:n}"1,000

Debugging with = (Python 3.8+)

Python 3.8 introduced the = specifier in f-strings, which automatically prints the expression and its value. This is invaluable for debugging.

python
x = 10
y = 20
print(f"{x + y = }")
# Output: x + y = 30

name = "Alice"
print(f"{name = }")
# Output: name = 'Alice'

import math
print(f"{math.pi = }")
# Output: math.pi = 3.141592653589793

# With format specifier
print(f"{math.pi = :.2f}")
# Output: math.pi = 3.14

# Debugging in complex expressions
result = {"key": "value", "count": 42}
print(f"{result.get('count') = }")
# Output: result.get('count') = 42

Debugging Patterns

python
# Debugging variables
name = "Alice"
age = 30
print(f"{name = }, {age = }")
# Output: name = 'Alice', age = 30

# Debugging function calls
def get_user():
    return {"name": "Bob", "age": 25}

print(f"{get_user() = }")
# Output: get_user() = {'name': 'Bob', 'age': 25}

# Debugging with format specifiers
x = 3.14159
print(f"{x = :.2f}")
# Output: x = 3.14

# Debugging boolean expressions
print(f"{10 > 5 = }")
# Output: 10 > 5 = True

Multiline f-strings

f-strings work with triple quotes, allowing you to create multiline formatted strings.

python
name = "Alice"
age = 30
city = "Paris"

# Triple-quoted f-string
message = f"""Name: {name}
Age: {age}
City: {city}"""

print(message)
# Output:
# Name: Alice
# Age: 30
# City: Paris

# Line continuation with backslash
formatted = (
    f"Name: {name}, "
    f"Age: {age}, "
    f"City: {city}"
)
print(formatted)
# Output: Name: Alice, Age: 30, City: Paris

Multiline Examples

python
# Multi-line dictionary formatting
data = {
    "name": "Alice",
    "age": 30,
    "city": "Paris",
    "country": "France"
}

formatted = f"""
User Information:
  Name: {data['name']}
  Age: {data['age']}
  Location: {data['city']}, {data['country']}
"""
print(formatted)

# Multi-line with alignment
items = [
    ("Apple", 1.29, 3),
    ("Bread", 2.49, 1),
    ("Milk", 3.99, 2),
]

header = f"{'Item':<15}{'Price':>8}{'Qty':>5}{'Total':>10}"
print(header)
print("-" * 40)

for name, price, qty in items:
    total = price * qty
    print(f"{name:<15}{price:>8.2f}{qty:>5}{total:>10.2f}")

Nested f-strings

You can nest f-strings inside other f-strings. Inner f-strings are evaluated first.

python
width = 10
text = "Hello"

# Nested f-string with alignment
print(f"|{f'{text}':^{width}}|")   # Output: |  Hello   |

# Nested with format specifiers
value = 42
print(f"|{f'{value:.2f}':>10}|")   # Output: |     42.00|

# Building a formatted table
data = [
    ("Alice", 95.5),
    ("Bob", 87.3),
    ("Charlie", 92.1),
]

header = f"|{'Name':^10}|{'Score':^10}|"
separator = f"|{'-'*10}|{'-'*10}|"
print(header)
print(separator)
for name, score in data:
    row = f"|{f'{name}':^10}|{f'{score:.1f}':^10}|"
    print(row)
# Output:
# |   Name   |  Score   |
# |----------|----------|
# |  Alice   |   95.5   |
# |   Bob    |   87.3   |
# | Charlie  |   92.1   |

# Using nested f-strings for conditional formatting
status = "active"
label = f"Status: {f'ACTIVE' if status == 'active' else 'INACTIVE'}"
print(label)
# Output: Status: ACTIVE

Nesting Patterns

python
# Nested with dictionary access
user = {"name": "Alice", "scores": [95, 87, 92]}
print(f"User: {user['name']}, Avg: {sum(user['scores']) / len(user['scores']):.1f}")

# Nested with list comprehension
numbers = [1, 2, 3, 4, 5]
print(f"Even: {[n for n in numbers if n % 2 == 0]}")

# Nested with function calls
def get_status(code):
    return "OK" if code == 200 else "Error"

print(f"Status: {get_status(200)}")

f-strings vs Other Methods

f-strings vs %-formatting

python
name = "Alice"
age = 30

# %-formatting (old style)
old_style = "Name: %s, Age: %d" % (name, age)

# f-string
new_style = f"Name: {name}, Age: {age}"

# Both output: Name: Alice, Age: 30
# f-string is more readable and doesn't require remembering type codes

f-strings vs .format()

python
name = "Alice"
age = 30

# .format() method
formatted = "Name: {}, Age: {}".format(name, age)

# .format() with named placeholders
formatted_named = "Name: {name}, Age: {age}".format(name=name, age=age)

# f-string
f_formatted = f"Name: {name}, Age: {age}"

# All output: Name: Alice, Age: 30
# f-string is more concise and easier to read

f-strings vs string.Template

python
from string import Template

name = "Alice"
age = 30

# Template
template = Template("Name: $name, Age: $age")
template_formatted = template.substitute(name=name, age=age)

# f-string
f_formatted = f"Name: {name}, Age: {age}"

# Both output: Name: Alice, Age: 30
# Template is safer for user-provided templates but more verbose

Method Comparison Table

Feature%.format()f-stringTemplate
ReadabilityLowMediumHighMedium
SpeedFastSlowFastestSlowest
SafetyLowMediumLowHigh
VerbosityMediumHighLowHigh
ExpressionsNoYesYesNo
DebuggingNoNoYes (=)No

Performance Comparison

f-strings are typically the fastest string formatting method in Python.

python
import timeit

name = "Alice"
age = 30
city = "Paris"

# % formatting
def percent_format():
    return "Name: %s, Age: %d, City: %s" % (name, age, city)

# .format() method
def format_method():
    return "Name: {}, Age: {}, City: {}".format(name, age, city)

# f-string
def fstring_format():
    return f"Name: {name}, Age: {age}, City: {city}"

# Benchmark each method
iterations = 1000000

percent_time = timeit.timeit(percent_format, number=iterations)
format_time = timeit.timeit(format_method, number=iterations)
fstring_time = timeit.timeit(fstring_format, number=iterations)

print(f"% formatting:      {percent_time:.4f}s")
print(f".format() method:  {format_time:.4f}s")
print(f"f-string:          {fstring_time:.4f}s")
# Typical output (varies by system):
# % formatting:      0.1200s
# .format() method:  0.1800s
# f-string:          0.0800s

f-strings are generally 2-3x faster than .format() and 20-30% faster than % formatting because they are evaluated at compile time where possible and avoid function call overhead.

Common Patterns

JSON-style Formatting

python
import json

user = {
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com"
}

print(json.dumps(user, indent=2))

# f-string for quick key-value formatting
print(f"User: {user['name']} ({user['age']})")
# Output: User: Alice (30)

for key, value in user.items():
    print(f"  {key}: {value}")
# Output:
#   name: Alice
#   age: 30
#   email: alice@example.com

SQL Query Building

python
table = "users"
columns = "id, name, email"
where_clause = "age > 25"

query = f"SELECT {columns} FROM {table} WHERE {where_clause}"
print(query)
# Output: SELECT id, name, email FROM users WHERE age > 25

# For production code, always use parameterized queries:
# cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))

Logging and Debug Messages

python
import logging

user_name = "Alice"
action = "login"
status = "success"

# f-string in logging (recommended for performance)
logging.info(f"User {user_name} performed {action}: {status}")

# Error messages with context
def process_payment(amount, currency):
    if amount <= 0:
        raise ValueError(f"Invalid payment amount: {amount} {currency}")
    return f"Processed {currency} {amount:.2f}"

try:
    process_payment(-50, "USD")
except ValueError as e:
    print(f"Error: {e}")
    # Output: Error: Invalid payment amount: -50 USD

Date and Time Formatting

python
from datetime import datetime

now = datetime.now()

print(f"Current date: {now:%Y-%m-%d}")
print(f"Current time: {now:%H:%M:%S}")
print(f"Full datetime: {now:%Y-%m-%d %H:%M:%S}")
print(f"Date: {now:%B %d, %Y}")

UI Formatting

python
# Progress bar
progress = 0.75
bar_length = 20
filled = int(bar_length * progress)
bar = "#" * filled + "." * (bar_length - filled)
print(f"Progress: [{bar}] {progress:.0%}")
# Output: Progress: [################....] 75%

# Status indicators
status = "active"
icon = "✓" if status == "active" else "✗"
print(f"Status: {icon} {status.upper()}")
# Output: Status: ✓ ACTIVE

Common Mistakes

1. Confusing f-string Braces with Dict or Set Literals

python
# Wrong: Python thinks these are format specifiers
# print(f"{'key': 'value'}")  # SyntaxError

# Solution: use variables for complex expressions
pair = {"key": "value"}
print(f"Dict: {pair}")
# Output: Dict: {'key': 'value'}

2. Using Backslashes Inside f-string Expressions

python
# Wrong: Backslashes not allowed inside f-string expressions (Python < 3.12)
# print(f"Newline: {'\n'}")  # SyntaxError in Python < 3.12

# Solution: Use variables or chr()
newline = "\n"
print(f"Newline:{newline}After")

# Or use chr()
print(f"Tab:{chr(9)}After")

3. Forgetting to Convert Types

python
# For complex objects, ensure they have a string representation
class MyClass:
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return f"MyClass(value={self.value})"

obj = MyClass(42)
print(f"Object: {obj}")
# Output: Object: MyClass(value=42)

4. Not Handling Missing Keys in Dictionaries

python
data = {"name": "Alice", "age": 30}

# Wrong: KeyError if key doesn't exist
# print(f"City: {data['city']}")  # KeyError

# Solution: Use .get() with default
print(f"City: {data.get('city', 'Unknown')}")
# Output: City: Unknown

5. Escaping Braces Incorrectly

python
# To include literal braces in an f-string, double them
print(f"Use {{curly braces}} in f-strings")
# Output: Use {curly braces} in f-strings

# This is an expression:
value = 42
print(f"{{This is an expression: {value}}}")
# Output: {This is an expression: 42}

6. Overusing f-strings in Logs

python
import logging

# BAD: f-string evaluated even if logging level disables it
logging.debug(f"Processing {len(data)} items")

# GOOD: Use % formatting for lazy evaluation
logging.debug("Processing %d items", len(data))

Practice Exercises

Exercise 1: Create a Formatted Receipt

Create a function that takes a list of items with prices and quantities, and returns a formatted receipt string.

python
def create_receipt(items, tax_rate=0.08):
    """Create a formatted receipt from a list of items."""
    receipt_lines = []
    receipt_lines.append("=" * 40)
    receipt_lines.append(f"{'RECEIPT':^40}")
    receipt_lines.append("=" * 40)
    receipt_lines.append(f"{'Item':<20}{'Qty':>5}{'Price':>8}{'Total':>8}")
    receipt_lines.append("-" * 40)

    subtotal = 0
    for item_name, price, quantity in items:
        item_total = price * quantity
        subtotal += item_total
        receipt_lines.append(
            f"{item_name:<20}{quantity:>5}{price:>8.2f}{item_total:>8.2f}"
        )

    tax = subtotal * tax_rate
    total = subtotal + tax

    receipt_lines.append("-" * 40)
    receipt_lines.append(f"{'Subtotal:':<30}{subtotal:>10.2f}")
    receipt_lines.append(f"{'Tax (' + f'{tax_rate:.0%}' + '):':<30}{tax:>10.2f}")
    receipt_lines.append("=" * 40)
    receipt_lines.append(f"{'TOTAL:':<30}{total:>10.2f}")
    receipt_lines.append("=" * 40)

    return "\n".join(receipt_lines)


items = [
    ("Apple", 1.29, 3),
    ("Bread", 2.49, 1),
    ("Milk", 3.99, 2),
    ("Eggs", 4.99, 1),
]

print(create_receipt(items))
# Output:
# ========================================
#               RECEIPT
# ========================================
# Item                 Qty   Price   Total
# ----------------------------------------
# Apple                  3    1.29    3.87
# Bread                  1    2.49    2.49
# Milk                   2    3.99    7.98
# Eggs                   1    4.99    4.99
# ----------------------------------------
# Subtotal:                      19.33
# Tax (8%):                       1.55
# ========================================
# TOTAL:                         20.88
# ========================================

Exercise 2: Build a Log Formatter

Create a function that formats log messages with timestamps, levels, and context information.

python
from datetime import datetime

def format_log(level, message, context=None):
    """Format a log message with timestamp, level, and optional context."""
    timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    colors = {
        "DEBUG": "\033[90m",
        "INFO": "\033[94m",
        "WARNING": "\033[93m",
        "ERROR": "\033[91m",
        "CRITICAL": "\033[95m",
    }
    reset = "\033[0m"

    context_str = f" [{context}]" if context else ""
    color = colors.get(level, "")
    formatted = f"{color}{timestamp} [{level}]{reset}{context_str}: {message}"

    return formatted


print(format_log("INFO", "Server started on port 8080"))
print(format_log("WARNING", "High memory usage", "system"))
print(format_log("ERROR", "Failed to connect to database", "db"))
print(format_log("DEBUG", f"Processing {1000} records", "worker"))

Exercise 3: Create a Table Formatter

Create a function that formats data as a nicely aligned ASCII table with customizable alignment.

python
def create_table(headers, rows, alignments=None):
    """Create a formatted ASCII table."""
    if alignments is None:
        alignments = ['<'] * len(headers)

    # Calculate column widths
    col_widths = []
    for i, header in enumerate(headers):
        width = len(str(header))
        for row in rows:
            width = max(width, len(str(row[i])))
        col_widths.append(width + 2)

    def format_cell(value, width, align):
        return f"{str(value):{align}{width}}"

    separator = "+" + "+".join("-" * w for w in col_widths) + "+"
    header_row = "|" + "|".join(
        format_cell(h, col_widths[i], alignments[i])
        for i, h in enumerate(headers)
    ) + "|"

    data_rows = []
    for row in rows:
        formatted_row = "|" + "|".join(
            format_cell(row[i], col_widths[i], alignments[i])
            for i in range(len(headers))
        ) + "|"
        data_rows.append(formatted_row)

    table_lines = [separator, header_row, separator] + data_rows + [separator]
    return "\n".join(table_lines)


headers = ["Name", "Age", "Score", "Grade"]
rows = [
    ["Alice", 25, 95.5, "A"],
    ["Bob", 30, 87.3, "B+"],
    ["Charlie", 22, 92.1, "A-"],
    ["Diana", 28, 78.9, "B"],
]
alignments = ['<', '^', '>', '^']

print(create_table(headers, rows, alignments))
# Output:
NameAgeScoreGradeAlice2595.5ABob3087.3B+Charlie2292.1A-Diana2878.9B

Exercise 4: Format Currency Values

Create a function that formats currency values with proper symbols and thousands separators.

python
def format_currency(amount, currency="USD", locale="en_US"):
    """Format a number as currency with proper symbols and separators."""
    symbols = {
        "USD": "$",
        "EUR": "€",
        "GBP": "£",
        "JPY": "¥",
    }
    
    symbol = symbols.get(currency, currency)
    
    if currency == "JPY":
        # JPY doesn't use decimals
        return f"{symbol}{amount:,.0f}"
    else:
        return f"{symbol}{amount:,.2f}"

# Test
print(format_currency(1234.56))           # $1,234.56
print(format_currency(1234.56, "EUR"))    # €1,234.56
print(format_currency(1234.56, "GBP"))    # £1,234.56
print(format_currency(1234.56, "JPY"))    # ¥1,235

Key Takeaways

  • f-strings are the preferred string formatting method in Python 3.6+ due to their readability, conciseness, and performance
  • Use the = specifier (Python 3.8+) for quick debugging to see both expression and value
  • Format specifiers follow the mini-language: [[fill]align][sign][z][#][0][width][grouping_option][.precision][type]
  • f-strings support any valid Python expression, including function calls, method calls, and comprehensions
  • Triple-quoted f-strings enable multiline formatted strings
  • Nested f-strings allow complex formatting patterns
  • Avoid backslashes inside f-string expressions (Python less than 3.12)
  • f-strings are typically 2-3x faster than .format() and faster than % formatting
  • Always handle missing keys in dictionaries using .get() with defaults
  • Use literal braces by doubling them: {{ and }}
  • Consider using % formatting for logging to enable lazy evaluation
  • f-strings are the most readable and maintainable string formatting method
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-09-28

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement