Chapter 14 of 20

Python f-Strings & String Formatting

When working with variables, we often need to put their values inside a sentence.

For example, instead of writing:

name = "John"
age = 20

print("My name is", name, "and I am", age, "years old.")

Python gives us a much cleaner way to do this using f-strings.

What are f-Strings?

f-strings are formatted strings that allow you to directly insert variables and expressions inside a string.

An f-string starts with f before the quotation mark:

name = "John"
age = 20

print(f"My name is {name} and I am {age} years old.")

Output:

My name is John and I am 20 years old.

Anything inside {} is evaluated by Python and its result is inserted into the string.

Using Expressions

You can also put expressions directly inside {}.

price = 100
quantity = 3

print(f"Total price: {price * quantity}")

Output:

Total price: 300

You don't need to create another variable just for the calculation.

Formatting Numbers

f-strings are especially useful when you want to control how numbers are displayed.

Decimal Places

Suppose we have:

price = 99.5678

print(f"Price: {price:.2f}")

Output:

Price: 99.57

.2f means display two digits after the decimal point.

Percentage

You can format a decimal as a percentage:

score = 0.85

print(f"Score: {score:.1%}")

Output:

Score: 85.0%

Formatting with Commas

For large numbers, commas can make the output easier to read.

population = 1400000000

print(f"Population: {population:,}")

Output:

Population: 1,400,000,000

Formatting Text

You can also control the alignment of text.

name = "John"

print(f"{name:>10}")
print(f"{name:<10}")
print(f"{name:^10}")

Here:

  • > → Align right

  • < → Align left

  • ^ → Center

This can be useful when creating neatly formatted output.

Other String Formatting Methods

Before f-strings became popular, Python commonly used other formatting techniques.

format() Method

You can use {} placeholders with .format():

name = "John"
age = 20

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

Output:

My name is John and I am 20 years old.

You can also use named placeholders:

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

% Formatting

Python also has an older formatting style using %:

name = "John"
age = 20

print("My name is %s and I am %d years old." % (name, age))

Output:

My name is John and I am 20 years old.

You may still see this style in older Python code, but for new code, f-strings are generally the preferred choice when you're using a modern Python version.

f-Strings with Functions

You can even call functions inside an f-string:

name = "john"

print(f"Welcome, {name.upper()}!")

Output:

Welcome, JOHN!

A Practical Example

Let's create a simple bill:

product = "Laptop"
price = 55000
quantity = 2

total = price * quantity

print(f"Product: {product}")
print(f"Price: ₹{price:,}")
print(f"Quantity: {quantity}")
print(f"Total: ₹{total:,}")

Output:

Product: Laptop
Price: ₹55,000
Quantity: 2
Total: ₹110,000

As you can see, f-strings make formatted output cleaner, shorter, and much easier to read. For most modern Python programs, they're the formatting technique you'll want to reach for first.