Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › format()

Python Reference

str.format()

Replace {} placeholders with values. The classic string formatting method.

Last updated: October 2026

Definition: str.format(*args, **kwargs) replaces {} placeholders in a string with the provided arguments. It supports positional and keyword arguments, plus format specifications for alignment, padding, and type conversion.

🎬 Live Example

Five common format patterns. Click any button to see the output.

Input:

                
Output:

            

💡 F-strings (3.6+) are the modern preferred way. format() is still useful for templates.

📝

Basic Syntax

"template {}".format(value)
"template {0} {1}".format(a, b)
"template {name}".format(name="Alice")

# Returns: new str
# Does NOT modify the original

🎯 Common Patterns

Pattern Example Result
Auto-index"{} {}".format(1, 2)'1 2'
Manual index"{1} {0}".format('a', 'b')'b a'
Keyword"{x}".format(x=5)'5'
Float"{:.2f}".format(3.14159)'3.14'
Alignment"{:>10}".format('hi')' hi'
1

Positional Arguments

"Hello, {}!".format("World")
# 'Hello, World!'

"{} + {} = {}".format(1, 2, 3)
# '1 + 2 = 3'

"{1} {0}".format("a", "b")
# 'b a'
2

Keyword Arguments

"{name} is {age}".format(name="Alice", age=30)
# 'Alice is 30'

# Reuse values
"{0} and {0}".format("X")
# 'X and X'

"{greeting}, {name}!".format(greeting="Hi", name="Bob")
# 'Hi, Bob!'
3

Number Formatting

"{:.2f}".format(3.14159)   # '3.14'
"{:,.2f}".format(1234567.89) # '1,234,567.89'
"{:d}".format(42)           # '42'
"{:>10}".format("hi")       # '        hi'
"{:<10}".format("hi")       # 'hi        '
"{:^10}".format("hi")       # '    hi    '
4

format vs f-string

name = "Alice"

# format()
"Hello, {}".format(name)

# f-string (preferred, 3.6+)
f"Hello, {name}"

# f-strings are faster and more readable
# Use format() for stored templates

Rule of thumb: Use f-strings for inline interpolation, format() for reusable templates.

5

Real-World: Email Template

template = """Dear {name},

Your order #{order_id} has shipped.
Total: ${total:.2f}

Thanks,
{company}
"""

message = template.format(
    name="Alice",
    order_id=12345,
    total=99.99,
    company="Acme Inc"
)

🎯 format vs f-string vs %

Method Version Use when
f-string3.6+Most inline formatting (preferred)
format2.6+Stored/reusable templates
%OldLegacy code only

⚠️ Common Mistakes

❓ Frequently Asked Questions

What does format() do in Python?

str.format() replaces placeholders {} in a string with the provided arguments. It supports positional and keyword arguments, plus format specifications for alignment, padding, and type conversion.

What is the difference between format and f-strings?

F-strings (Python 3.6+) are more concise and faster for inline interpolation. format() is still useful when the template is defined separately from the values or when you need to reuse the same template multiple times.

How do I use positional arguments in format?

Use empty braces {} for automatic order, or numbered braces {0} {1} to reference arguments by index. '{1} {0}'.format('a', 'b') returns 'b a'.

How do I use keyword arguments in format?

Pass keyword arguments and reference them in braces: '{name} is {age}'.format(name='Alice', age=30). This is useful for templates where the order of arguments is unclear.

Does format modify the original string?

No. Strings are immutable in Python. format returns a new string.

🎯 What's Next?

← All Python Reference