BeginnerLesson firstCategory 7 of 20

String Formatting

Format and interpolate strings with f-strings and .format(). Read the lesson first, then move through the exercises in order.

8 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with F-String Interpolation

Study Material

Read the full lesson

Moving beyond plus signs

In the last lesson, you learned how to combine text and variables using the + sign.

python
name = \"Leo\" age = 25 print(\"My name is \" + name + \" and I am \" + str(age) + \" years old.\")

That works, but it is messy. You have to remember to add spaces, and you have to manually convert numbers to strings using str(), otherwise Python crashes.

There is a much better way.

Meet the f-string

Python 3.6 introduced f-strings (formatted strings). They are completely revolutionary for writing clean code.

To make an f-string, you simply place an f right before the first quote.

Then, you can put variables directly inside curly braces {}.

python
name = \"Leo\" age = 25 print(f\"My name is {name} and I am {age} years old.\")

This is much easier to read and write. Python automatically converts the integer 25 into text for you.

Math inside f-strings

You do not just have to put variables in the curly braces. You can put actual Python expressions in them.

python
price = 10 quantity = 3 print(f\"Your total is ${price * quantity}.\")

Python will calculate price * quantity, get 30, and then place it directly into the string.

Making decimals look like money

F-strings have a hidden superpower: formatting data.

Imagine you divide 10 by 3.

python
print(f\"The result is {10 / 3}\")

This prints The result is 3.3333333333333335.

If you are displaying a price or a precise measurement, that looks terrible.

You can fix this by adding a colon : and a format specifier inside the curly braces.

python
value = 10 / 3 print(f\"The result is {value:.2f}\")

The :.2f means "format this as a float with 2 decimal places."

The output is now The result is 3.33. It even rounds the number for you correctly!

Padding numbers with zeros

Sometimes you want numbers to line up neatly, or you are generating ID numbers that must all have exactly 5 digits.

You can tell the f-string to pad the number with zeros by using :05d.

python
order_id = 42 print(f\"Order Number: {order_id:05d}\")

This prints Order Number: 00042.

The 0 means "pad with zeros". The 5 means "make it 5 characters wide". The d means "treat this as an integer (decimal)".

Aligning text

F-strings can also push text to the left, right, or center of an empty space. This is great for drawing tables in the console.

  • < aligns left
  • > aligns right
  • ^ aligns center
python
word = \"Python\" print(f\"|{word:<15}|\") # Left align in a 15-character space print(f\"|{word:^15}|\") # Center align print(f\"|{word:>15}|\") # Right align

This produces:

|Python         |
|    Python     |
|         Python|

The old .format() method

Before f-strings existed, Python used the .format() method.

You might see this in older code or tutorials.

python
print(\"Hello, {}. You are {}.\".format(name, age))

It works by finding the empty {} markers and filling them with the variables inside .format(), in order.

While it is good to recognize this approach, you should use f-strings for all your new code. They are faster and much easier to read.

What this lesson should give you

After this lesson, you should understand how to:

  • create an f-string by placing an f before the quotes
  • inject variables directly into text using {}
  • format decimal numbers nicely using :.2f
  • pad numbers with zeros using :05d
  • align text to create neat visual columns

F-strings are one of Python's most beloved features. You will use them constantly to build messages, logs, and outputs.

Interactive

Exercises for this topic

These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.