f-strings in Python Programming Language

If you’ve ever tried to combine text and variables in Python, you know it can feel a little awkward at first. Older methods work, but they’re often messy and hard to read. That’s exactly why f-strings exist.

Introduced in Python 3.6, f-strings are now the recommended and most popular way to format strings. They are simple, powerful, and much easier to understand—especially for beginners.

In this guide, I’ll explain f-strings in plain language, with clear examples and practical tips you can start using right away.


What Are f-Strings in Python?

An f-string (short for formatted string) lets you insert variables and expressions directly inside a string.

All you have to do is:

  1. Add the letter f before the string
  2. Place variables or expressions inside {}

A Basic Example

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

Output:

My name is Ali and I am 25 years old.

No extra symbols. No confusing syntax. Just clean, readable code.


Why f-Strings Are Better Than Older Methods

Before f-strings, Python developers used methods like string concatenation or .format(). These still work, but they’re not ideal anymore.

Old Way: String Concatenation

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

Problems:

  • Harder to read
  • Requires converting numbers to strings
  • Easy to make mistakes

Old Way: .format()

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

Better, but still not as clean as f-strings.

Modern Way: f-Strings

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

This is why most modern Python code uses f-strings.


Using Expressions Inside f-Strings

One powerful feature of f-strings is that they can evaluate expressions—not just variables.

a = 10
b = 5
print(f"The sum is {a + b}")

Output:

The sum is 15

You can use:

  • Math operations
  • Function calls
  • Calculations

All inside the curly braces.


Formatting Numbers with f-Strings

f-strings also make number formatting very easy.

Decimal Places

price = 49.98765
print(f"The price is {price:.2f}")

Output:

The price is 49.99

Adding Commas to Large Numbers

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

Output:

Population: 123,456,789

These small features make your output look much more professional.


Working with Dates and Time

f-strings work beautifully with dates.

from datetime import datetime
today = datetime.now()
print(f"Today's date is {today:%Y-%m-%d}")

Output example:

Today's date is 2026-01-25

This is especially useful in reports, logs, and automation scripts.


Multi-Line f-Strings

You can use f-strings across multiple lines, which is great for messages or reports.

name = "Sara"
score = 92
message = f"""
Student Name: {name}
Final Score: {score}
Status: Passed
"""
print(message)

This keeps your code clean and easy to edit.


Common Mistakes to Avoid

Even though f-strings are simple, beginners often make these mistakes:

1. Forgetting the f

print("Hello {name}") # Wrong

Python will print {name} instead of the value.

2. Using Quotes Incorrectly

print(f'My name is {'Ali'}') # Error

Use different quote types or escape them properly.

3. Using f-Strings in Older Python Versions

f-strings only work in Python 3.6 and above. If you’re using an older version, they won’t work.


When Should You Use f-Strings?

Use f-strings when:

  • Printing messages
  • Creating logs
  • Formatting output
  • Writing clean, readable code

In modern Python development, f-strings should be your default choice for string formatting.


Key Takeaways

  • f-strings are the simplest and cleanest way to format strings in Python
  • They allow variables and expressions inside {}
  • They improve readability and reduce errors
  • They are faster and more modern than older formatting methods
  • If you’re writing Python today, f-strings are a must-know skill

Mastering f-strings may seem like a small thing, but it makes your code clearer, more professional, and easier to maintain.

If you enjoy learning practical programming skills, communication techniques, and ways to grow professionally, you might find my books on personal development and business skills helpful. You can explore them on Apple Books.


Discover more from Shafaat Ali Education

Subscribe to get the latest posts sent to your email.

Leave a comment

Recent posts

apple books

Buy my eBooks on Apple Books. Thanks! Shafaat Ali, Apple Books

Discover more from Shafaat Ali Education

Subscribe now to keep reading and get access to the full archive.

Continue reading