Skip to content

Python concatenation

  • by

In Python, concatenation refers to the process of combining two or more strings together. Python provides several ways to concatenate strings, making it easy to build longer strings from smaller ones.

Here are the main syntax options for string concatenation in Python:

1. Using the + operator:

The + operator can be used to concatenate two strings, resulting in a new string that contains the characters of both input strings.

string1 = "Hello"
string2 = "World"
result = string1 + string2

2. Using the += operator:

The += operator is used to concatenate a string to an existing string, modifying the original string in place.

string1 = "Hello"
string2 = "World"
string1 += string2

3. Using the join() method:

The join() method is used to concatenate elements of an iterable (e.g., a list of strings) into a single string, where you can specify a separator to be placed between each element.

words = ["Hello", "World"]
result = " ".join(words)  # You can specify any separator you want between the words

4. Using f-strings (Python 3.6 and above):

F-strings provide a concise and convenient way to concatenate variables and expressions within a string literal.

string1 = "Hello"
string2 = "World"
result = f"{string1} {string2}"

5. Using the str.format() method (Python 2 and 3):

The str.format() method allows you to insert variable values and expressions into a string, making it a form of string concatenation.

string1 = "Hello"
string2 = "World"
result = "{} {}".format(string1, string2)

6. Using the str.concat() method (less common):

The str.concat() method is less common and is used to concatenate two strings in a similar way to the + operator.

string1 = "Hello"
string2 = "World"
result = string1.__add__(string2)

All of these syntax options perform string concatenation and create a new string object containing the concatenated result. The choice of which method to use depends on your specific use case and coding style preferences. For most cases, the + operator, join(), and f-strings are the most commonly used and recommended options.

Python concatenation example

Here’s an example demonstrating various methods of string concatenation in Python:

# Using the + operator
str1 = "Hello"
str2 = "World"
result1 = str1 + str2
print("Using + operator:", result1) 

# Using the += operator
str3 = "Python"
str4 = "Programming"
str3 += str4
print("Using += operator:", str3)

# Using the join() method
words = ["Welcome", "to", "Python", "Programming"]
result2 = " ".join(words)
print("Using join() method:", result2)

# Using f-strings
name = "John"
age = 30
result3 = f"My name is {name} and I am {age} years old."
print("Using f-strings:", result3)

# Using the str.format() method
item = "apple"
quantity = 5
result4 = "I have {} {}s.".format(quantity, item)
print("Using str.format() method:", result4)

# Using the str.concat() method
str5 = "Hello"
str6 = " World"
result5 = str5.__add__(str6)
print("Using str.concat() method:", result5)

Output:

Python concatenation

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading