My Notes

The f Thing in Python - f-strings

Updated 6/28/20264 min readDownload PDFEdit

The f Thing in Python — f-strings

So what even is it?

Okay so imagine you're writing a text message to a friend and you want to include their name and age in it. You could write it out manually every single time. Or — you could just drop in a blank and let Python fill it in for you.

That's what an f-string does.

Official definition: An f-string (formatted string literal) is a string prefixed with f or F that lets you embed Python expressions directly inside {} curly braces, evaluated at runtime.

In human words: you put an f before your quote, and then anything you wrap in {} inside that string gets replaced with the actual value.


The simplest possible example

name = "Jordan"
age = 22

print(f"Hello, {name}! You are {age} years old.")

Output:

Hello, Jordan! You are 22 years old.

That's it. {name} got swapped with "Jordan", and {age} got swapped with 22. Python did the work.


The story — why does this exist?

Before f-strings came along (they were added in Python 3.6), people were doing this to combine text and variables:

The old way — string concatenation with +

name = "Jordan"
age = 22

print("Hello, " + name + "! You are " + str(age) + " years old.")

Output:

Hello, Jordan! You are 22 years old.

Same result. But look at how annoying that is. You have to:

  • manually add + everywhere
  • manually call str() to convert the number, because you can't concatenate a string and an integer directly
  • count your spaces carefully so words don't smushed together

One small mistake and you get an error. It's fragile, it's ugly, it doesn't scale.

The other old way — .format()

name = "Jordan"
age = 22

print("Hello, {}! You are {} years old.".format(name, age))

Output:

Hello, Jordan! You are 22 years old.

Better than +, but still annoying — you write {} in the string and then separately list the values in .format(). The bigger the string, the harder it is to match which {} maps to which variable.

Then f-strings showed up

name = "Jordan"
age = 22

print(f"Hello, {name}! You are {age} years old.")

Clean. Direct. The variable is right there inside the string where it belongs. No str(), no +, no .format(). You just read it left to right and it makes sense.


You can put expressions in there too, not just variables

This is where it gets actually interesting. Inside {} you can put any valid Python expression — math, method calls, conditions, anything.

price = 49.99
quantity = 3

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

Output:

Total: $149.97

The :.2f part is a format specifier — it rounds the number to 2 decimal places. You don't need to know all of these right now, but it's good to know they exist.

name = "jordan"

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

Output:

Hi, JORDAN!

.upper() is a string method that converts to uppercase — and you can call it right inside the curly braces.


All three approaches, side by side

ApproachCodeWorks since
Concatenation (+)"Hello " + name + "!"Always
.format()"Hello {}!".format(name)Python 2.6
f-stringf"Hello {name}!"Python 3.6

If you're on Python 3.6 or newer (which you almost certainly are), use f-strings. They're the current standard.


When should you actually use each one?

Use f-strings when:

  • You're printing messages, logs, or output to the screen
  • You're building any string that includes variable data
  • You want clean, readable code — which is basically always
username = "kalyan"
score = 95

print(f"User {username} scored {score}/100.")

Use .format() when:

  • You're working with older Python code (pre-3.6) and need consistency
  • You're building a reusable template string that gets filled in later
template = "Dear {name}, your order #{order_id} has been shipped."
print(template.format(name="Jordan", order_id=4821))

Use + concatenation almost never — it's the most error-prone and the hardest to read. You'll mostly see it in very old Python code or when someone just learned the language.


One more real example, putting it all together

name = "Kalyan"
language = "Python"
days = 3

print(f"Hey {name}, you've been learning {language} for {days} days.")
print(f"At this rate, in {days * 10} days you'll be dangerous.")

Output:

Hey Kalyan, you've been learning Python for 3 days.
At this rate, in 30 days you'll be dangerous.

The one-line summary

Put f before a string, wrap your variables in {}, and Python replaces them with real values. That's f-strings. Everything else is just extra power on top of that core idea.