String Formatting

Learn how to format strings in Python using format() and f-strings with examples.

Loading...
String Formatting

So far, we’ve learned how to work with strings, variables, functions and modules. But what if you want to combine variables with text and display everything neatly?

That’s where string formatting comes in.

String formatting helps you create readable output when working with variables, numbers, and dynamic content.

Python provides a few ways to format strings. Let’s look at them.

1. Using the format() Method

The format() method lets you insert values into a string using curly braces {} as placeholders.

For example:

name = "Shefali"
country = "India"
 
text = "Hey, my name is {} and I am from {}"
print(text.format(name, country))
 
# Output:
# Hey, my name is Shefali and I am from India

Here’s how it works:

  • {} marks where each value should go.
  • .format(name, country) passes the values into the placeholders in order.
  • The first {} receives name, and the second receives country.

Changing the Order of Values

By default, values are inserted in the order you provide them.

For example:

name = "Shefali"
country = "India"
 
text = "Hey, my name is {} and I am from {}"
print(text.format(country, name))
 
# Output:
# Hey, my name is India and I am from Shefali

The values appear in the wrong places because we passed country first and name second.

To control which value goes into each placeholder, you can use index numbers:

name = "Shefali"
country = "India"
 
text = "Hey, my name is {1} and I am from {0}"
print(text.format(country, name))
 
# Output:
# Hey, my name is Shefali and I am from India

Indexing starts at 0, so {0} refers to the first value and {1} refers to the second.

Using Named Placeholders

You can also give placeholders names, which makes your code easier to read.

txt = "For only {price:.2f} dollars!"
print(txt.format(price=49))
 
# Output:
# For only 49.00 dollars!

Here, {price:.2f} means:

  • price is the name of the value.
  • .2f formats the number to two decimal places.

This is useful when displaying prices or other decimal numbers.

2. Padding and Alignment

Sometimes, you want your output to look neat by aligning text within a fixed space.

Python lets you align text to the left, right, or center using format specifications.

text = "Python"
 
# Left align
print("{:<10}!".format(text))
 
# Right align
print("{:>10}!".format(text))
 
# Center align
print("{:^10}!".format(text))
 
# Output:
 
# Python    !
#    Python!
#  Python  !

Let’s break down "{:<10}":

  • : starts the format specification.
  • < aligns the text to the left.
  • > aligns the text to the right.
  • ^ centers the text.
  • 10 sets the total field width to 10 characters.
  • The exclamation mark is outside the field, so it appears after the aligned text.

3. Using f-Strings

The format() method works well, but Python also provides a shorter and more readable option: f-strings.

Introduced in Python 3.6, f-strings let you insert variables and expressions directly into a string.

Just add f before the opening quotation mark and place your variables inside {}.

For example:

name = "Shefali"
country = "India"
 
text = f"Hey, my name is {name} and I am from {country}"
print(text)
 
# Output:
# Hey, my name is Shefali and I am from India

Notice that we don’t need to call .format().

We can place the variable names directly inside the string.

Using Expressions in f-Strings

You can also perform calculations inside the curly braces.

print(f"{2 * 30}")
 
# Output:
# 60

You can even format numbers to a specific number of decimal places:

price = 49
 
txt = f"For only {price:.2f} dollars!"
print(txt)
 
# Output:
# For only 49.00 dollars!

Just like with format(), .2f displays the number with two decimal places.

Padding and Alignment With f-Strings

You can use the same alignment options with f-strings.

text = "Python"
 
print(f"{text:<10}!")  # Left align
print(f"{text:>10}!")  # Right align
print(f"{text:^10}!")  # Center align
 
# Output:
 
# Python    !
#    Python!
#  Python  !

The alignment symbols work the same way. The difference is that f-strings let you place the variable directly inside the string.

format() vs f-Strings

Both methods can format strings, insert variables, and control how numbers are displayed.

Here’s the main difference:

name = "Shefali"
 
# Using format()
print("Hello, {}".format(name))
 
# Using an f-string
print(f"Hello, {name}")

Both print: Hello, Shefali

For most everyday string formatting, f-strings are a great choice because they’re concise and easy to read. You’ll still come across the format() method in existing Python code.


👉 Next tutorial: Python Docstrings