← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 3 of 16

Python Format Strings

The format method is like a fill-in-the-blank sentence: you write the sentence with empty slots and then hand over the words to fill them. It lets you build messages with changing values.
Syntax
python
"text {} and {}".format(value1, value2)
"text {name}".format(name=value)

Basic .format() Method

The .format() method replaces {} placeholders in a string with the arguments you pass to it, in the order they appear — "{} is {}".format(name, age) — letting you build readable strings without manually concatenating pieces with +.

Example: Basic .format() Method

python
name = "Alex"
age = 30
print("{} is {}".format(name, age))  # {} placeholders filled in order

Named Placeholders

Giving placeholders names instead of leaving them positional, like "{name} is {age}".format(name=name, age=age), makes the template self-documenting and immune to bugs caused by arguments being passed in the wrong order.

Example: Named Placeholders

python
name = "Alex"
age = 30
print("{name} is {age}".format(name=name, age=age))  # named placeholders, order-independent

Formatting Decimals

Adding a colon and format spec inside the braces, such as {:.2f}, controls how a number is displayed — in this case, rounding a float to exactly two decimal places, which is essential for anything involving currency or percentages.

Example: Formatting Decimals

python
price = 19.999
print("{:.2f}".format(price))

Alignment and Padding

Field-width and alignment specifiers let you pad values to a fixed width and choose < for left, > for right, or ^ for center alignment — the standard technique for lining up columns of numbers or text in console output.

Example: Alignment and Padding

python
print("{:<10}|".format("left"))  # < left-aligns within a width of 10
print("{:>10}|".format("right"))  # > right-aligns within a width of 10
print("{:^10}|".format("mid"))  # ^ centers within a width of 10

Reusing Arguments

Including index numbers inside the braces, like "{0} {1} {0}", lets a single argument be reused multiple times or displayed out of the order it was passed, without duplicating the value in the function call itself.

Example: Reusing Arguments

python
print("{0} {1} {0}".format("echo", "middle"))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Providing fewer arguments than placeholders, e.g. "{} {}".format(a), which raises IndexError.
  2. Mixing automatic {} and manual {0} numbering in one string, which raises ValueError.
  3. Using the wrong format spec such as "{:d}".format(3.5), which raises ValueError; use {:.1f} for floats.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.