Python format() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Python, format() builds a new string by filling {} placeholders with values you pass.

You can use positional fields, named fields, or both. Format specifiers after a colon control width, precision, and alignment.

Syntax

Call format on a template string. Start with this line.

python
msg = "Hello, {}!".format(name)
CallWhat it does
"{} {}".format("Ada", "Lovelace")Fills positional fields.
"{name}".format(name="Ada")Fills a named field.

For one-off text, f-strings are often clearer. Keep format() for reusable templates.

Library declarations
python
str.format(*args, **kwargs) → str

Parameters

ParameterDescription
*argsOptional. Positional values for {} or {0}, {1}.
**kwargsOptional. Named values for {name} placeholders.

Return Value

Return ValueDescription
strA new string with placeholders replaced.

Python Examples

Example 1 — Positional Placeholders

Empty {} slots fill left to right from the arguments.

python
msg = "{}, {} and {}".format("red", "green", "blue")
print(msg)

# reuse / reorder with indexes
print("{1} then {0}".format("first", "second"))

How It Works

  1. Three empty braces. They take args 0, 1, and 2 in order.
  2. Result joins cleanly. Output is one readable sentence.
  3. Numbered fields. {1} and {0} pick arguments by index.
  4. Reorder without rewriting data. Handy for templates that share values.

Example 2 — Named Placeholders

Keyword arguments make templates easier to read and maintain.

python
template = "{user} has {points} points"
print(template.format(user="Ada", points=120))

# names can appear more than once
print("{x} + {x} = {y}".format(x=2, y=4))

How It Works

  1. Name each slot. {user} and {points} match keywords.
  2. Pass keywords in any order. Matching is by name, not position.
  3. Reuse a name. {x} appears twice with the same value.
  4. Clearer templates. Often better than many bare {} slots.

Example 3 — Format Specifiers

Control precision, alignment, and separators after the colon.

python
price = 1234.5
print("{:.2f}".format(price))
print("{:>10}".format("hi"))
print("{:,}".format(1000000))

# same ideas with an f-string:
# f"{price:.2f}"

How It Works

  1. .2f. Forces two decimal places (adds a trailing 0).
  2. >10. Right-aligns in a field 10 characters wide.
  3. ,. Inserts a thousands separator.
  4. Same mini-language. Specifiers also work inside f-strings.

Frequently Asked Questions

It replaces placeholders in a template with the values you provide.
A new str. The template is not modified.
f-strings embed values in the literal. format() keeps the template separate.
Use a specifier after a colon, such as {:.2f} or {:,}.
format_map(mapping) fills named placeholders from a mapping object.

Next: format_map()

Format a template using keys from a mapping/dict.

format_map() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful