How to insert a variable into a string in Python

Python provides you with four options to insert a variable into a string:

  1. Using the f-string format
  2. The str.format() method
  3. The old % formatting syntax
  4. The regular concatenation with the + operator

This tutorial will show you examples of how to use the four options above in practice.

1. Inserting variables with the f-string format

The formatted string literal or f-string format is the best way to insert variables into a string because it’s intuitive and written beautifully.

To use the f-string format, you only need to add a literal f before the string you want to write. Here’s an example of inserting name and age variables into a string:

name = "Nathan"
age = 29

my_str = f"I'm {name} and I'm {age} years old."

print(my_str)

Output:

I'm Nathan and I'm 29 years old.

As you can see, this option is intuitive because you can insert a variable right into the string just by adding curly brackets {} around the variable.

But keep in mind that this option is available only in Python version 3.6 and up. If you need to support older Python versions, you need to use the other options.

2. Using the str.format() method

The str.format() method is a popular option to insert a variable into a string before the addition of the f-string format.

When using this option, you need to add curly braces in the string {} that will be replaced with the values you passed to the format() method.

Here’s an example of using the str.format() method:

name = "Nathan"
age = 29

my_str = "I'm {} and I'm {} years old.".format(name, age)

print(my_str)

Output:

I'm Nathan and I'm 29 years old.

Python will read the string and insert the variables you specified as the format() arguments into the curly brackets added in the string.

This approach is quite intuitive, but the f-string format is clearly better.

Insert variables with the old % formatting syntax

Another option to insert variables into a string is to use the % operator.

When using this option, you need to include the % operator followed by a format specifier to let Python know how the variables should be inserted.

By the end of the string, you need to add another % operator and specify the values you want to put in the string. See the example below:

name = "Nathan"
age = 29

my_str = "I'm %s and I'm %s years old." % (name, age)

print(my_str)

Output:

I'm Nathan and I'm 29 years old.

As you can see, two %s formats are included in the string, which then gets replaced with the two values written after the string.

This approach is less intuitive because the format looks like magic spells, but it works with older Python versions, so you might see it used frequently in legacy source code.

4. The regular concatenation with the + operator

The last option to insert a variable into a string is to use the concatenation + operator.

To use this option, you need to break the string and insert the variable using the concatenation operator as follows:

name = "Nathan"
age = 29

my_str = "I'm " + name + " and I'm " + str(age) + " years old."

print(my_str)

Output:

I'm Nathan and I'm 29 years old.

One weakness of this method is that Python will raise an error when you concatenate a non-string type to the string.

You need to convert any non-string variable into a string with the str() function so that Python can concatenate it together.

This option also doesn’t allow you to format the output as in the other three options, so it’s good when you simply want to print the variable values.

Conclusion

Python provides four different ways to insert a variable into a string. The f-string format is the latest improvement which is available in Python v3.6 and above. Currently, it’s the best way to insert a variable into a string.

If you need to support older Python versions, you can use any other three options in your code.

I hope this tutorial is helpful. Happy coding! 👍

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.