Last modified: Sep 05, 2023 By Alexander Williams
Python: Add Variable to String & Print Using 4 Methods
Python offers different approaches to concatenate variables and strings depending on the use case.
I'd like to share some commonly used methods with you in this tutorial.
1. String concatenation
String concatenation is the process of combining two or more strings into a single string.
We can use this method to add a variable to a string. Here is an example:
# Variable
name = "Pytutorial"
# Insert Variable to String
greeting = "Hello, " + name + "!"
# Print Result
print(greeting)
Output:
Hello, Pytutorial!
Please keep in mind that you may get a TypeError
if your variable is a number. Here's what I mean:
# Variable
name = 33
# Insert Variable to String
greeting = "Hello, " + name + "!"
# Print Result
print(greeting)
Output:
TypeError: can only concatenate str (not "int") to str
So, how do we fix this issue?
To fix this issue, we need to convert the integer variable to a string using the str()
function before concatenating it to the string.
# Variable
name = 33
# Insert Variable to String
greeting = "Hello, " + str(name) + "!"
# Print Result
print(greeting)
2. % operator
The %s
operator can also be used to add a variable to a string.
# Variable
name = "Pytutorial"
# Insert Variable to String
greeting = "Hello, %s!" % name
# Print Result
print(greeting)
Output:
Hello, Pytutorial!
The good thing about this method is that if the variable is a number, it is unnecessary to convert it. In addition, you can insert multiple variables into a string.
# Variables
name = "Pytutorial"
description = "Python Tutorials"
# add Variables to String
greeting = "Hello, %s! Do you like %s?" % (name, description)
# Print Result
print(greeting)
Output:
Hello, Pytutorial! Do you like Python Tutorials?
To add the integer variable, use %d
:
# Variables
name = "Pytutorial"
age = 22
# Insert Variables into String
greeting = "Hello, %s! You are %s years old" % (name, age)
# Print Result
print(greeting)
Output:
Hello, Pytutorial! You are 22 years old
3. the .format()
The str.format()
method in Python is used for string formatting and is an alternative to the older %
operator for string interpolation.
The following example shows how to use The str.format()
method to add variables to a string;
# Variables
name = "Pytutorial"
age = 22
# Insert Variables into String using .format()
formatted_string = "Hello, {}! You are {} years old.".format(name, age)
# Print Result
print(formatted_string)
Output:
Hello, Pytutorial! You are 22 years old.
You can also use positional arguments and specify the index of the values to be inserted:
formatted_string = "Hello, {1}! You are {0} years old.".format(age, name)
Additionally, you can use named placeholders for increased readability:
formatted_string = "Hello, {person_name}! You are {person_age} years old.".format(person_name=name, person_age=age)
4. f-strings (Python 3.6+)
In my opinion, the most understandable way to insert a variable into a string is by using f-strings
. Let's look at how it works.
# Variables
name = "Pytutorial"
age = 22
# Insert Variables into String using f-string
formatted_string = f"Hello, {name}! You are {age} years old."
# Print Result
print(formatted_string)
Output:
Hello, Pytutorial! You are 22 years old.
Please remember that the f-string method is only compatible with Python 3.6 or higher.
Conclusion
Well done! Now, you have learned the standard methods to add variables or variables to the string.
Choosing the right mehod depends on what you're trying to do, but knowing these techniques will give you the power to manipulate strings well.