How to Insert a Python Variable into a String?

As a Python developer, I got a requirement to insert variables into strings. There are several methods to achieve this, In this tutorial, I will explain how to insert a Python variable into a string using various methods with examples.

To insert a Python variable into a string using the f-string method, simply prefix your string with an f or F and place the variable inside curly braces {} within the string. For example, if you have a variable name = "John", you can use print(f"My name is {name}.") to include the variable in the output.

There are various methods to put a Python variable into a string. Let me explain each method with examples.

1. Using the Comma Method

The comma method is easy to understand and use. It involves separating the string and the variable with a comma in the print function.

Syntax:

Here is the syntax:

print("string", variable)

Example:

Let me show you an example. Below is the Python code.

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

Output:

My name is John and I am 28 years old.

Here is the output you can see in the screenshot below:

python variable in string

Check out Split a String into Equal Parts in Python

2. Using the Modulus Operator (%)

The modulus operator is an older method but is still widely used. It allows you to insert variables into a string by using % followed by a tuple of variables in Python.

Syntax:

Here is the syntax:

"string %s" % variable

Example:

Let me show you an example.

city = "New York"
temperature = 75
print("The temperature in %s is %d degrees Fahrenheit." % (city, temperature))

Output:

The temperature in New York is 75 degrees Fahrenheit.

Here is the exact output in the screenshot below:

python string with variables

Read Split a String by Index in Python

3. Using the format() Method

The format() method in Python allows you to insert variables into strings using curly braces {} as placeholders.

Syntax:

Here is the syntax:

"string {}".format(variable)

Example:

Now, let me show you an example.

first_name = "Alice"
last_name = "Johnson"
print("Hello, my name is {} {}.".format(first_name, last_name))

Output:

Hello, my name is Alice Johnson.

Here is the output in the screenshot below:

python variable inside string

Check out How to Compare Strings in Python?

4. Using F-Strings (Formatted String Literals)

F-strings are the most recent and recommended method for inserting variables into strings in Python. They are concise and easy to read, introduced in Python 3.6.

Syntax:

Here is an example.

f"string {variable}"

Example:

Let me show you an example.

state = "California"
population = 39500000
print(f"The population of {state} is approximately {population} people.")

Output:

The population of California is approximately 39500000 people.

Here is the output in the screenshot below:

python create string with variables

Check out Convert String to List in Python Without Using Split

5. Using String Concatenation

String concatenation involves using the + operator to combine strings and variables in Python. This method requires converting non-string variables to strings using the str() function.

Syntax:

Here is the syntax:

"string" + str(variable)

Example:

Here is an example.

event = "concert"
tickets = 150
print("We sold " + str(tickets) + " tickets for the " + event + ".")

Output:

We sold 150 tickets for the concert.

Here is the exact output in the screenshot below:

variable inside string python

Conclusion

In this tutorial, I explained, how to insert variables into strings in Python programming using various methods—comma, modulus operator, format(), f-strings, and string concatenation etc. If you still have questions, feel free to comment below.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.