Python String Concatenation

In python, you can combine or concatenate strings by using the + operator, format() method, join() method, etc., based on our requirements.

 

Now, we will learn how to use different approaches to concatenate strings in python.

String Concatenation with + Operator

In python, you can use the + operator to add or concatenate strings together based on your requirements.

 

Following is the example of using the + operator to append/concatenate strings in python.

 

a = "Welcome"
b = "to"
c = "tutlane"
d = a + b + c
e = a + " " + b + " " + c
print(d)
print(e)

If you observe the above example, we are appending/concatenating the multiple string variables (a, b, c) by using + operator and adding space between them by using " ".

 

When you execute the above python example, you will get the result as shown below.

 

Welcometotutlane
Welcome to tutlane

This is how you can use the + operator in python to concatenate multiple strings based on your requirements.

 

In python, if you want to concatenate multiple copies of the same string, you can use the * operator like as shown below.

 

a = "Python"
print(a * 3) #PythonPythonPython

Python Concatenate String and Int

In python, concatenating string and int values are not allowed, and it will throw an exception.

 

Following is the example of concatenating the string and int using the + operator in python.

 

a = "Welcome"
b = 120
c = a + b
print(c)

When you execute the above python program, you will get the result as shown below.

 

Traceback (most recent call last):
  File "D:/Python Examples/pythonstringformatting.py", line 3, in <module>
    c = a + b
TypeError: can only concatenate str (not "int") to str

In python, concatenating string and int values are not allowed. If you want to concatenate string and number (int) values, you need to convert a number (int) value to a string using the str() function.

 

a = "Welcome"
b = 120
c = a + str(b)
print(c) #Welcome120

If you observe the above example, we are concatenating the string and int values by converting the number (int) to a string using the str() function.

 

When you execute the above python program, you will get the result as shown below.

 

To know more about type conversion in python, refer to python type conversion.

String Concatenation with format() Method

In python, the string format() method is useful for string formatting as well as for string concatenation.

 

msg1 = "{} to {}".format("Welcome","Tutlane")
print(msg1)
msg2 = "{} {}".format("Learn", "Python")
print(msg2)

If you observe the above example, we used a string format() method to concatenate multiple strings based on our requirements.

 

When you execute the above python program, you will get the result as shown below.

 

Welcome to Tutlane
Learn Python

To learn more about using the format() method in string concatenation, refer python string format() method.

Concatenate Strings with join() Method

In python, you can also use string join() method to concatenate strings but the join() method will work with only iterable objects like list, tuple, etc. 

 

Following is the example of concatenating the strings using the join() method in python.

 

lst = ("Welcome", "to", "Tutlane")
msg1 = "-".join(lst)
print(msg1)
msg2 = ",".join(lst)
print(msg2)
msg3 = " ".join(lst)
print(msg3)

If you observe the above example, we are concatenating the strings using join() method with different separators.

 

When you execute the above python program, you will get the result as shown below.

 

Welcome-to-Tutlane
Welcome,to,Tutlane
Welcome to Tutlane

String Concatenation with % Operator

Same as join() and format() methods, the formatting operator % is also useful to concatenate the strings in python.

 

a = "Welcome to"
b = "Tutlane"
msg1 = "%s %s" % (a,b)
print(msg1)
msg2 = "%s %s" %("Learn", "Python")
print(msg2)

If you observe the above example, we are concatenating the strings using the % operator.

 

When you execute the above python program, you will get the result as shown below.

 

Welcome to Tutlane
Learn Python

String Concatenation with f-string

If you are using the python 3.6+ version, you can use f-string to concatenate strings.

 

Following is the example of concatenating the strings using f-string in python.

 

a = "Welcome"
b = "Tutlane.com"
str1 = f"{a} to {b}"
print(str1)
name = "Suresh"
age = 33
str2 = f"My name is {name}, and I am {age} years old"
print(str2)

This is how you can concatenate strings in python using different approaches based on your requirements.