You are currently viewing How to Join Strings in Python

How to Join Strings in Python

Joining strings in Python is a common operation. In fact, it’s one of the first things you learn when starting to code in Python. There are many ways to join strings in Python, but the most common way is by using the + operator. This method is simple and straightforward, but it has some limitations. For example, if you’re joining a lot of strings together, this method can become very slow.

Joining strings in Python is a common operation. In fact, it’s one of the first things you learn when starting to code in Python. There are many ways to join strings in Python, but the most common way is by using the + operator. This method is simple and straightforward, but it has some limitations. For example, if you’re joining a lot of strings together, this method can become very slow.

Another way to join strings in Python is by using the join() method. The join() method is useful when you have a list of strings that you want to join together into a single string. The syntax for the join() method is: string.join(list).

Method 1: Join Strings using + Operator

str_1 = "Pixel is a square and "
str_2 = "a smallest building blocks of any image."

print(str_1 + str_2)

Output:

Pixel is a square and a smallest building blocks of any image.

Method 2: Join Strings using join()

str_1 = "Pixel is a square and "
str_2 = "a smallest building blocks of any image."
print(''.join([str_1, str_2]))

Output:

Pixel is a square and a smallest building blocks of any image.

References:

https://docs.python.org/3/

Leave a Reply