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.
This is how to create a string with variables in Python using the string concatenation method.
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:
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.
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:
- String concatenation
- String formatting with ‘%’
str.format()
- 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:
- How to Create a String with Newline in Python
- Create a String of N Characters in Python
- Split a Sentence into a List of Words in Python
- How to Split a String Using Regex in Python
- Variable in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.