str.format()

Added in v2.6 · Updated March 13, 2026 · String Methods
stdlib string methods formatting

The .format() method formats values into strings using placeholder notation. It gives you more control than simple concatenation or f-strings, especially when you need format specifiers, named placeholders, or conditional formatting.

Signature

str.format(*args, **kwargs)

Parameters

ParameterTypeDefaultDescription
*argspositionalRequiredValues to substitute into positional placeholders {}
**kwargskeywordRequiredValues to substitute into named placeholders {name}

Return Value

Returns a new string with placeholders replaced by provided values.

Basic Examples

Positional placeholders

greeting = "Hello, {}!"
print(greeting.format("World"))  # Output: Hello, World!

# Multiple placeholders
template = "{} has {} apples"
print(template.format("Alice", 5))  # Output: Alice has 5 apples

Named placeholders

template = "{name} is {age} years old"
print(template.format(name="Bob", age=30))  # Output: Bob is 30 years old

Reusing placeholders

template = "{0} likes {1}, and {0} really likes {1}"
print(template.format("Carol", "Python"))  # Output: Carol likes Python, and Carol really likes Python

Format Specifiers

Numbers

# Floating point precision
print("{:.2f}".format(3.14159))  # Output: 3.14

# Thousands separator
print("{:,}".format(1000000))  # Output: 1,000,000

# Percentage
print("{:.1%}".format(0.75))  # Output: 75.0%

# Pad with zeros
print("{:05d}".format(42))  # Output: 00042

Alignment

# Left, center, right alignment
print("{:>10}".format("text"))  # Output:      text
print("{:<10}".format("text"))  # Output: text     
print("{:^10}".format("text"))  # Output:    text

Strings

# Truncate
print("{:.5}".format("Hello World"))  # Output: Hello

# Pad strings
print("{:>10}".format("Hi"))  # Output:         Hi

Practical Use Cases

Table formatting

data = [
    ["Name", "Age", "City"],
    ["Alice", "28", "NYC"],
    ["Bob", "35", "LA"],
]

for row in data:
    print("{:<10} {:>5} {:<10}".format(*row))
# Output:
# Name            Age City      
# Alice            28 NYC       
# Bob              35 LA 

Currency formatting

def format_currency(amount):
    return "${:,.2f}".format(amount)

print(format_currency(1234.5))   # Output: $1,234.50
print(format_currency(1000000))  # Output: $1,000,000.00

Date formatting

from datetime import datetime

dt = datetime(2026, 3, 13, 14, 30)
print("Date: {:%Y-%m-%d}".format(dt))  # Output: Date: 2026-03-13
print("Time: {:%H:%M}".format(dt))     # Output: Time: 14:30
print("Full: {:%B %d, %Y}".format(dt)) # Output: Full: March 13, 2026

Conditional formatting

score = 85
print("Score: {passing}".format(passing="PASS" if score >= 60 else "FAIL"))
# Output: Score: PASS

f-strings vs .format()

Both achieve similar results. f-strings (Python 3.6+) are more concise for simple cases:

name = "Alice"
# f-string
f"Hello {name}"
# .format()
"Hello {}".format(name)

Use .format() when you need dynamic format strings, compatibility with older Python versions, or when building format strings programmatically.

See Also