Python String format() Method

The Python string format() method inserts values into a string by replacing placeholders.

It is a useful way to build readable messages without joining many strings together by hand.

Quick example #

name = "Maya"
age = 12

message = "My name is {} and I am {} years old.".format(name, age)
print(message)

Output:

My name is Maya and I am 12 years old.

Use {} as placeholders, then pass values to format() in the same order.

If you are new to strings, see Python strings explained: basics and examples.

What the format() method does #

The format() method:

  • inserts values into a string
  • replaces placeholders like {} with given values
  • returns a new string
  • does not change the original string

Example:

text = "Hello, {}"
new_text = text.format("Sam")

print(new_text)
print(text)

Output:

Hello, Sam
Hello, {}

Notice that text stays the same. format() creates and returns a new string.

Basic syntax #

The general form is:

"text {}".format(value)

Key idea:

  • each {} is a placeholder
  • values are inserted from left to right by default
  • the number of placeholders should match the values you pass

Example:

animal = "cat"
sound = "meow"

sentence = "The {} says {}.".format(animal, sound)
print(sentence)

Output:

The cat says meow.

You can print the result directly, or save it in a variable first.

Using positional placeholders #

You can use empty braces {} for simple left-to-right replacement.

print("I like {} and {}.".format("pizza", "pasta"))

Output:

I like pizza and pasta.

You can also use numbered placeholders like {0} and {1}.

This is helpful when:

  • you want to reuse a value
  • you want to change the order

Example with reused values:

text = "{0} is learning Python. {0} likes coding.".format("Lina")
print(text)

Output:

Lina is learning Python. Lina likes coding.

Example with changed order:

text = "{1} comes after {0}.".format("one", "two")
print(text)

Output:

two comes after one.

Numbered placeholders work, but for beginners, plain {} is often easier to read unless you need to reuse values.

Using named placeholders #

Named placeholders look like {name}.

You pass matching names into format():

text = "My name is {name} and I live in {city}.".format(name="Ana", city="Lima")
print(text)

Output:

My name is Ana and I live in Lima.

Named placeholders are useful because:

  • they make code easier to read
  • they help when a string has many values
  • they reduce confusion about the order

Example:

report = "Student: {student}, Score: {score}".format(student="Noah", score=95)
print(report)

Output:

Student: Noah, Score: 95

If you need to convert a value to text first, see Python str() function explained.

Formatting numbers #

format() can also control how numbers look.

A common example is :.2f, which means:

  • show the value as a floating-point number
  • keep 2 digits after the decimal point

Example:

price = 3.5
text = "Price: ${:.2f}".format(price)
print(text)

Output:

Price: $3.50

Another example:

average = 91.236
print("Average: {:.2f}".format(average))

Output:

Average: 91.24

This is useful for:

  • prices
  • averages
  • percentages

You can print formatted text with the Python print() function.

Return value #

format() returns a string.

That means you can:

  • save it in a variable
  • print it directly
  • use it anywhere a string is needed

Example:

message = "Hello, {}!".format("Chris")
print(message)
print(type(message))

Output:

Hello, Chris!
<class 'str'>

The original string is not changed.

format() vs f-strings #

format() is a string method.

Python also has f-strings, which are another way to insert values into strings.

Example with format():

name = "Rita"
print("Hello, {}!".format(name))

Example with an f-string:

name = "Rita"
print(f"Hello, {name}!")

For many beginners, f-strings are shorter and easier to read.

This page focuses on how format() works. If you want the bigger picture, read how to format strings in Python.

Common mistakes #

Here are some common problems beginners run into when using format().

Using more placeholders than values #

This causes an error because Python cannot fill every placeholder.

text = "Hello, {} {}!".format("Sam")
print(text)

You need to pass enough values for all placeholders.

Fixed version:

text = "Hello, {} {}!".format("Sam", "Lee")
print(text)

Using the wrong placeholder name #

The name inside the braces must match the name passed to format().

Problem:

text = "Hello, {name}!".format(username="Sam")
print(text)

Fixed version:

text = "Hello, {name}!".format(name="Sam")
print(text)

Expecting format() to change the original string #

format() does not modify the original string.

Problem:

text = "Hello, {}"
text.format("Sam")
print(text)

Output:

Hello, {}

Fixed version:

text = "Hello, {}"
text = text.format("Sam")
print(text)

Forgetting that format() returns a new string #

This is similar to the problem above. Always store the result if you need to use it later.

Mixing placeholder indexes in a confusing way #

Numbered placeholders can be useful, but too much mixing can make the string harder to understand.

Use simple {} or named placeholders when possible.

If you are trying to join strings and numbers directly, you may also see TypeError: can only concatenate str not int to str.

Helpful checks while debugging #

These simple lines can help you test what is happening:

print(text)
print(type(value))
print("Hello {}".format("Sam"))
help(str.format)

Use them to check:

  • what your string looks like before and after formatting
  • what type your value has
  • whether a small test example works
  • Python’s built-in help for str.format

FAQ #

What does Python string format() return? #

It returns a new string with the placeholders replaced.

Do I need to use numbers inside the braces? #

No. You can use plain {} for simple cases, or numbered and named placeholders when needed.

Can I format numbers with format()? #

Yes. A common example is "{:.2f}" to show 2 decimal places.

Is format() better than f-strings? #

For many beginners, f-strings are easier to read, but format() is still useful and common.

See also #

Press Esc to close