Python String Concatenation

python-string-concatenation-ipcisco-2

In Python Strings, we can combine two or more strings together. This is called Python String Concatenation or python string append. In other words with String Concatenation, we can add one more strings to another one. So how can we use Python String Concatenation?

 

There are two ways for Python String Concatenation. In this lesson, we will learn two of these Python String Concatenation techniques. One of them is using a plus (+) sign and comibne strings. The other way is using join() python function.  Let’s start with the first one, with plus (+) sign.

 

 


You can also learn what a Python String Contains


 

 

String Concatenation With Plus (+) Operator

 

In Python, we can combine two or more strings together with Plus (+) operation. This is the simplest way to combine one more python strings. So how to use plus (+) sign for Python String Concatenation?

 

The usage of plus (+) sign is very easy. Basically, we use this sign between the strings. This can be two string or more than two strings.

 

For example if we have below strings that are assigned to the below variables:

 

x = "Hello"
y = "Python"
z = "World"
a = x + y + z
print(a)

 

The output of the above python string append code will be:

 

HelloPythonWorld

 

python-string-concatenation-ipcisco-1

 

You can also watch the video of this lesson!

As you can see above, the strings are combined together without any space. If you would like to add a space between them, we should also add space string between them.

 

x = "Hello"
y = "Python"
z = "World"
a = x + " " + y + " " + z
print(a)

 

The output of this python string append will be like below:

 

Hello Python World

 

python-string-concatenation-ipcisco-2

 

As you can see, this time, the spaces are added between the strings.

 

We can also use this plus (+) sign for Python String Concatenation in print funtion.

 

print ("Hello" + "Python" + "World")

 

HelloPythonWorld

 

Again, to add space between strings, we can use space strings like below:

 

print ("Hello" + " " + "Python"+ " " + "World")

 

Hello Python World

 

String Concatenation With Join() Function

 

For Python String Concatenation, we can also use a special python function named join(). With join() function, we can combine multiple strings together lke we do with plus (+) operator. Let’s give a fantastic example fort he usage of this python function.

 

characters = ["Aragorn", "Arwen", "Legolas", "Gimli", "Frodo"]

 

print ("".join(characters))

 

As you can see above, we have a variable named characters. And we assign a string list to this variable. And with print function, we print the output. In the print function, we use python join() function with the parameter characters. The output of this code will be like below:

 

AragornArwenLegolasGimliFrodo

 

python-string-concatenation-join-method

 

We can also use a special character between these strings. Let’s use comma (,) between these fantastic characters. To do this, we will write comma (,) between double quotes in print function.

 

characters = ["Aragorn", "Arwen", "Legolas", "Gimli", "Frodo"]
print (",".join(characters))

 

Aragorn,Arwen,Legolas,Gimli,Frodo

 

python-string-join-method-ipcisco-1

 

There is also a function named split() that divides a string or in other words it does python string split. This is opposite of python join () function.

 

This is basically Python String Concatenation or Python String Append. As a summary we can combine python strings with both plus (+) sign and with python join() function. You can also check the other examples in this Python Course and combine them with this method.

 

Back to: Python Programming Course > Python Strings

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Programming Course

Collapse
Expand
Latest Blog Posts