How to Format Strings in Python

String formatting in Python means putting values into text.

This is useful when you want to build messages, labels, filenames, or printed output. For example, you might want to combine text with variables like a name, price, or age.

For most beginners, the best option is an f-string because it is simple and easy to read.

Quick answer

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

Output:

My name is Sam and I am 25 years old.

Use an f-string for the simplest and most readable way to insert values into text.

What string formatting means

String formatting means:

  • Putting values into a string
  • Combining text and variables
  • Creating clear output for users or for yourself

For example, if you have variables like this:

name = "Mia"
age = 30

You may want to create a message like:

"Mia is 30 years old."

If you are new to strings, see Python strings explained with basics and examples.

Use f-strings for most cases

An f-string is a string with an f before the opening quote.

Inside the string, put variables or expressions in curly braces {}.

name = "Alex"
age = 19

message = f"{name} is {age} years old."
print(message)

Output:

Alex is 19 years old.

You can also use simple expressions inside the braces:

item = "notebook"
price = 2.5
quantity = 4

message = f"Total for {item}: ${price * quantity}"
print(message)

Output:

Total for notebook: $10.0

Why f-strings are usually the best choice:

  • Easy to read
  • Short and clear
  • Good for beginners
  • Common in modern Python code

f-strings work in Python 3.6 and later.

Format numbers inside f-strings

You can control how numbers appear inside an f-string.

A common example is showing 2 decimal places:

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

Output:

Price: $3.46

Here, :.2f means:

  • : starts the format part
  • .2 means 2 digits after the decimal point
  • f means format as a floating-point number

This is useful for:

  • Money
  • Averages
  • Percentages
  • Scores

Another example:

average = 87.1289
print(f"Average score: {average:.2f}")

Output:

Average score: 87.13

Use str.format() if needed

Another way to format strings is with the .format() method.

You place {} inside the string, then pass values to .format().

name = "Lena"
age = 22

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

Output:

My name is Lena and I am 22 years old.

This style is older than f-strings, but you will still see it in many examples and existing code.

You can also use numbered placeholders:

message = "First: {0}, Second: {1}".format("apple", "banana")
print(message)

Output:

First: apple, Second: banana

If you want a method-focused explanation, see Python string format() method.

Use concatenation only for simple cases

Concatenation means joining strings with +.

first_name = "Sam"
last_name = "Lee"

full_name = first_name + " " + last_name
print(full_name)

Output:

Sam Lee

This works well for very small string joins.

But if one value is not already a string, you must convert it first:

age = 25
message = "Age: " + str(age)
print(message)

Output:

Age: 25

The str() function converts a value to a string. If needed, read Python str() function explained or how to convert an int to a string in Python.

For larger messages, concatenation is usually less readable than f-strings.

Choose the right method

A simple rule:

  • Use f-strings for most new Python code
  • Use .format() when reading or updating older code
  • Use + only for very small string joins

Here is the same message written all 3 ways:

name = "Nora"
score = 95

f-string

message = f"{name} scored {score} points."
print(message)

.format()

message = "{} scored {} points.".format(name, score)
print(message)

Concatenation

message = name + " scored " + str(score) + " points."
print(message)

All 3 work, but the f-string version is usually the easiest to read.

Common errors when formatting strings

Here are some common mistakes beginners make.

Forgetting the f before an f-string

If you forget the f, Python does not replace the values.

name = "Sam"
message = "Hello, {name}"
print(message)

Output:

Hello, {name}

Fix it by adding the f:

name = "Sam"
message = f"Hello, {name}"
print(message)

Trying to join a string and an integer with +

This causes an error:

age = 25
message = "Age: " + age
print(message)

Python will raise a TypeError because age is an integer, not a string.

Fix it like this:

age = 25
message = "Age: " + str(age)
print(message)

Or better:

age = 25
message = f"Age: {age}"
print(message)

If you see this error, read TypeError: can only concatenate str (not "int") to str.

Using the wrong number of placeholders in .format()

If your string has placeholders, make sure you provide enough values.

message = "{} is {} years old".format("Mia", 30)
print(message)

This is correct.

But if the number of placeholders and values does not match, your code may fail or produce the wrong result.

Confusing print() arguments with actual string formatting

This prints multiple values:

name = "Sam"
age = 25

print(name, age)

Output:

Sam 25

This is useful, but it does not create one formatted string.

If you want a single string, do this:

name = "Sam"
age = 25

message = f"{name} is {age} years old."
print(message)

FAQ

What is the best way to format strings in Python?

For most beginners and modern code, use f-strings because they are simple and easy to read.

Can I put numbers inside a string?

Yes. Use an f-string or convert the number with str() before concatenating.

What does :.2f mean in an f-string?

It formats a number as a float with 2 digits after the decimal point.

Should I use + or .format()?

Use + only for very simple joins. Use .format() mainly when working with older code. Prefer f-strings for new code.

See also