Concatenate Strings in Python

String concatenation in Python refers to the process of joining two or more strings together. Python provides multiple methods to achieve string concatenation. In this tutorial, we will go through each of the method with an example.

Concatenate Strings using the + Operator

The simplest way to concatenate strings is using the + operator.

In the following example, we use + operator and concatenate the strings "Hello", " ", and "World".

</>
Copy
str1 = "Hello"
str2 = "World"
result = str1 + " " + str2
print(result)  # Output: Hello World

Output


Concatenate Strings using the join() Method

The join() method is efficient for concatenating multiple strings.

</>
Copy
words = ["Hello", "World"]
result = " ".join(words)
print(result)  # Output: Hello World

Output


Concatenate Strings using f-strings (Python 3.6+)

Formatted string literals, or f-strings, provide a clean way to concatenate strings.

</>
Copy
name = "John"
age = 25
result = f"My name is {name} and I am {age} years old."
print(result)  # Output: My name is John and I am 25 years old.

Output


String Concatenation using format() Method

The format() method allows string formatting and concatenation.

</>
Copy
name = "Alice"
city = "Paris"
result = "My name is {} and I live in {}.".format(name, city)
print(result)  # Output: My name is Alice and I live in Paris.

Output


String Concatenation using += Operator

The += operator appends a string to an existing string.

</>
Copy
message = "Hello"
message += " World"
print(message)  # Output: Hello World

Output


String Concatenation using map() and join()

For concatenating non-string elements, use map() with join().

</>
Copy
numbers = [1, 2, 3]
result = " - ".join(map(str, numbers))
print(result)  # Output: 1 - 2 - 3

Output