Last modified: Jan 19, 2023 By Alexander Williams

6 Easy Methods to Concatenate String and Variables Python

Python provides multiple ways to concatenate strings and variables. In this article, we will explore Python's different methods of concatenating strings and variables.

1. "+" operator

The "+" operator is Python's most basic and widely used method for concatenating strings and variables. It is used to add two or more strings or variables together. For example:

name = "John" # Variable

print("My name is " + name + "and I am 30 years old.")

Output:

My name is Johnand I am 30 years old.

In the above example:

  • the string is "My name is "
  • the variable name
  • the string is " and I am 30 years old."

The + operator is used to concatenate the strings and the variable together, which looks like this: "My name is John and I am 30 years old."

Note: You will face TypeError: can only concatenate str (not "int") to str if you try to concatenate strings with integers. For example, this code will raise the error:

name = "John" # Variable
age = 30 # Integer

print("My name is " + name + "and I am "  + age + " years old.")

Output:

TypeError: can only concatenate str (not "int") to str

So why does the error occur? Because the "+" operator can only concatenate two strings together, not a string and an integer.

To fix this, you need to convert the integer to a string using the str() function before concatenating it. See the example below:

name = "John"
age = 30
print("My name is " + name + " and I am " + str(age) + " years old.")

This will give the correct output "My name is John and I am 30 years old."

2. "," operator

The "," operator can also concatenate strings and variables in Python. It is similar to the "+" operator but automatically adds a space between the concatenated elements. For example:

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

Output:

My name is John and I am 30 years old.

Good news:  "," operator in a print statement or when concatenating a string with an integer does not require using the str() function to convert integers or other non-string data types to strings.

3. "join()" function

The join() function is a method used to join a list of strings with a specified delimiter which means we can use it to concatenate strings and variables.

Example:

# Assign the value "John" to the variable name
name = "John"

# Assign the value 30 to the variable age
age = 30

# Create a list called info containing strings and variables
info = ["My name is", name, "and I am", str(age), "years old."]

# Use the join() method to join the strings in the list
print(" ".join(info))

Output:

My name is John and I am 30 years old.
  1. Assign the value "John" to the variable name
  2. Assign the value 30 to the variable age
  3. Create a list called info containing strings and variables
  4. Convert the variable age to string using str()
  5. Use the join() method to join the strings in the list info with a space " " as the delimiter
  6. Print the result

4. formatted string literals (f-strings)

In Python, f-strings provide a way to embed expressions inside string literals using {}. This feature conveniently concatenates variables and strings without using the "+" or "," operators.

In the following example, you can see how o use the f-string:

name = "Alice"
age = 30
print(f"My name is {name} and I am {age} years old.")

As you can see, The f before the string indicates that it is an f-string.

Output:

My name is Alice and I am 30 years old.

Note: f-strings are available starting from Python 3.6 and later.

5. "format()" method

The format() method is similar to f-strings, allowing you to concatenate strings and variables. The syntax is slightly different.
However, let"s see an example:

name = "Alice"
age = 30
print("My name is {} and I am {} years old.".format(name, age))

Output:

My name is Alice and I am 30 years old.

It is also worth noting that format() method is available in all versions of Python.

6. "%" operator

Another way to concatenate variables and strings is to use the % operator. This method is similar to the format() method but uses the "%" symbol instead. For example:

name = "John"
age = 30
print("My name is %s and I am %d years old." % (name, age))

Output:

My name is John and I am 30 years old.

Conclusion

In this article, we have explored Python's different methods of concatenating strings and variables.

The "+" operator, "," operator, "join()" function, f-strings, format() method, and % operator are all pretty good methods to concatenate strings and variables in Python.

Finally, each method has advantages and disadvantages, and the choice will depend on the specific use case and personal preference.