Python Reference Library →

Python str.format() – Format Strings Easily with Placeholders

Let’s look at str.format() – format strings easily with placeholders

  1. Notice

    str.format() replaces fields in a format string. The result is a new string.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
name = 'Ada'
message = "Hello, {}!".format(name)
print(message)
Live state

Top-level scope

name→Object 1
message→Object 2
Object 1 · str'Ada'Object 2 · str'Hello, Ada!'
This step

str.format() replaces fields in a format string. The result is a new string.

Picture str.format() – format strings easily with placeholders

str.format() replaces fields in a format string. The result is a new string.

Image brief

Alt text
A diagram for Python str.format() – Format Strings Easily with Placeholders. Instructions: name = 'Ada' message = "Hello, {}!".format(name) print(message). Displayed result: Hello, Ada!. The final example bindings are name --> object 1 (str); message --> object 2 (str). object 1: 'Ada'; object 2: 'Hello, Ada!'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python str.format() – Format Strings Easily with Placeholders". Teaching focus: str.format() replaces fields in a format string. The result is a new string. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS name = 'Ada' message = "Hello, {}!".format(name) print(message) | v DISPLAYED RESULT Hello, Ada! FINAL BINDINGS name --> object 1 (str) message --> object 2 (str) object 1: 'Ada' object 2: 'Hello, Ada!' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python str.format() – Format Strings Easily with Placeholders. Instructions: name = 'Ada' message = "Hello, {}!".format(name) print(message). Displayed result: Hello, Ada!. The final example bindings are name --> object 1 (str); message --> object 2 (str). object 1: 'Ada'; object 2: 'Hello, Ada!'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
name = 'Ada'
message = "Hello, {}!".format(name)
print(message)
Live state

Top-level scope

name→Object 1
message→Object 2
Object 1 · str'Ada'Object 2 · str'Hello, Ada!'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
Hello, Ada!

What do you expect this program to print?

What does this example display?

name = 'Ada'
message = "Hello, {}!".format(name)
print(message)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

Hello, Ada!

Follow the printed values in order. The output is: Hello, Ada!

More examples and reference

Python str.format() Method

The str.format() method in Python lets you insert values into a string using curly braces {} as placeholders. It’s a powerful and flexible way to build dynamic strings.

Syntax

"string with {} placeholders".format(values...)

Parameters:

  • values... – One or more values to be inserted into the string in the order of the placeholders.

Returns:

  • A new string with the placeholders replaced by the provided values.

Example 1: Basic Usage

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

Example 2: Multiple Placeholders

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

Example 3: Using Index Numbers

sentence = "The {1} is {0} years old.".format(10, "car")
print(sentence)
The car is 10 years old.

Why? The numbers inside {} refer to the index of values passed into format().

Example 4: Named Placeholders

info = "Name: {name}, Age: {age}".format(name="Charlie", age=25)
print(info)
Name: Charlie, Age: 25

Example 5: Formatting Numbers

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

Why? :.2f formats the float to 2 decimal places.

Common Use Cases

  • Displaying user data in templates or messages
  • Logging or outputting debug information
  • Formatting numbers, dates, or currency

Common Mistakes

  • Mismatch between number of placeholders and arguments
  • Using incompatible types (e.g., formatting a list without converting)

Interview Tip

In coding interviews, use str.format() when building readable strings, especially in questions involving string construction or reporting.

Summary

  • str.format() lets you insert variables into strings using {}
  • You can use automatic order, index numbers, or named placeholders
  • You can apply formatting inside the placeholders

Practice Problem

Write a program that takes a name and score from the user and prints a sentence using str.format():

name = input("Enter your name: ")
score = input("Enter your score: ")
print("Hello, {}. You scored {} points.".format(name, score))

What to remember

Connect what you noticed in the example with the idea below.

Remember

str.format() replaces fields in a format string. The result is a new string.

Watch out for this idea

str.format() changes the format string in place.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →