How to Create a String with Variables in Python [Create a String with Multiple Variables in Python]

In this Python tutorial, I will explain in detail how to create a string with variables in Python. We will see different ways to create strings with variables in Python. We will also see, how to create a string with multiple variables in Python.

Create a String with Variables in Python

To create a string with variables in Python, we can use various methods like String concatenation, String formatting with ‘%’, str.format(), and f-strings. Let us check every method with examples.

Method-1: Create a String with Variables using String concatenation in Python

Let us see now, how to use string concatenation to create a string with variables in Python.

You can use the ‘+’ operator to concatenate strings with variables in Python. However, you must ensure that the variable is of string type. If not, you can convert it to a string using the str() function.

city = "New York"
state = "New York"
population = 8550405

output = "The city of " + city + ", " + state + " has a population of " + str(population) + "."
print(output)

Output:

The city of New York, New York has a population of 8550405.

See the complete code.

Create a String with Variables in Python
Create a String with Variables in Python

This is how to create a string with variables in Python using the string concatenation method.

READ:  How to convert string to boolean in Python? [6 Methods]

Method-2: Create a String with Variables in Python using String Formatting with ‘%’

Now, let us check another way to create a string with variables in Python using String Formatting with ‘%’.

city = "Los Angeles"
state = "California"
population = 3977686

output = "The city of %s, %s has a population of %d." % (city, state, population)
print(output)

Output:

The city of Los Angeles, California has a population of 3977686.

Method-3: Create a String with Variables in Python using str.format()

We can also use the str.format() Python to create a string with variables in python.

city = "Chicago"
state = "Illinois"
population = 2720156

output = "The city of {0}, {1} has a population of {2}.".format(city, state, population)
print(output)

Output:

The city of Chicago, Illinois has a population of 2720156.

You can also use named placeholders and pass them as keyword arguments:

output = "The city of {city}, {state} has a population of {population}.".format(city=city, state=state, population=population)
print(output)

Method-4: Create a String with Variables in Python using f-strings

Python 3.6 introduced f-strings, which allow you to embed expressions inside string literals, using curly braces {}. They are also known as “formatted string literals”.

city = "Houston"
state = "Texas"
population = 2313421

output = f"The city of {city}, {state} has a population of {population}."
print(output)

Output:

The city of Houston, Texas has a population of 2313421.

Create a String with Multiple Variables in Python

Now, we will see, how to create a string with multiple variables in Python using different formatting techniques.

1. Using Python ‘+’ Operator

You can concatenate strings with multiple variables using the ‘+’ operator in Python. Here’s an example:

city1 = "New York"
city2 = "Los Angeles"
city3 = "Chicago"

result = city1 + ", " + city2 + ", " + city3
print(result)

You can see the output below:

create string with multiple variables in Python
create string with multiple variables in Python

2. Using %-formatting

Another way to create a string with multiple variables is by using %-formatting in Python. Here’s how you can do it:

city1 = "New York"
city2 = "Los Angeles"
city3 = "Chicago"

result = "%s, %s, %s" % (city1, city2, city3)
print(result)

Output:

New York, Los Angeles, Chicago

3. Using str.format():

You can also use the Python str.format() method to create a string with multiple variables. Here’s an example:

city1 = "New York"
city2 = "Los Angeles"
city3 = "Chicago"

result = "{}, {}, {}".format(city1, city2, city3)
print(result)

Output:

New York, Los Angeles, Chicago

4. Using f-strings

In Python 3.6 and later, you can use f-strings to create a string with multiple variables. Here’s how:

city1 = "New York"
city2 = "Los Angeles"
city3 = "Chicago"

result = f"{city1}, {city2}, {city3}"
print(result)

Output:

New York, Los Angeles, Chicago

We have checked four different ways to create a string with multiple variables in Python. The ‘+’ operator, %-formatting, and str.format() methods are available in most versions of Python, while f-strings are available from Python 3.6 onwards.

READ:  Python Program to convert String to a List

Conclusion

Throughout this tutorial, we explored four different methods for creating strings with variables in Python, using examples of city names from the United States of America:

  1. String concatenation
  2. String formatting with ‘%’
  3. str.format()
  4. f-strings (Formatted String Literals)

Each method has its own advantages and use cases, but f-strings are generally recommended for their readability, simplicity, and versatility. Here we learned, how to create strings with variables in Python.

We also discussed different ways to create a string with multiple variables in Python.

You may also like the following string tutorials: