What is the preferred way to concatenate a string in Python?


The preferred way to concatenate a string in Python is by using the + operator or the join() method. Here's a step-by-step explanation of each method −

Using the + operator

To concatenate two strings using the + operator, simply put the strings next to each other with a + sign in between them.

Example

In this example, we concatenate the name variable with the greeting string using the + operator. The resulting string is "Hello, John!".

name = "John"
greeting = "Hello, " + name + "!"
print(greeting)

Output

Hello, John!

Using multiple + operators

You can also concatenate more than two strings by using multiple + operators −

Example

In this example, we concatenate the name and age variables with the greeting string using multiple + operators. Note that we use the str() function to convert the age variable to a string before concatenating it with the other strings.

name = "John"
age = 30
greeting = "Hello, my name is " + name + " and I am " + str(age) + " years old."
print(greeting)

Output

Hello, my name is John and I am 30 years old.

Using the join() method

To concatenate a list of strings using the join() method, first create a list of the strings you want to concatenate, and then call the join() method on a separator string with the list as an argument.

Example

In this example, we create a list of three strings called words. We then call the join() method on a space " " with the words list as an argument. The resulting string is "Hello world !".

words = ["Hello", "world", "!"]
sentence = " ".join(words)
print(sentence)

Output

Hello world !

Using the join() method with a custom separator

You can also use the join() method to concatenate strings with a custom separator −

Example

In this example, we create a list of three strings called words. We then call the join() method on a comma and space ", " with the words list as an argument. The resulting string is "apple, banana, orange".

words = ["apple", "banana", "orange"]
fruit_list = ", ".join(words)
print(fruit_list)

Output

apple, banana, orange

Using the + operator

Example

In this example, we concatenate the first_name and last_name strings with a space " " in between them using the + operator. The resulting string is "John Doe".

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)

Output

John Doe

Using the join() method

Example

In this example, we create a list of three strings called names. We then call the join() method on a comma and space ", " with all but the last element of the names list as an argument, and concatenate the resulting string with the last element of the names list and a greeting string using the + operator. The resulting string is "Hi, John, Jane and Bob!".

names = ["John", "Jane", "Bob"]
greeting = "Hi, " + ", ".join(names[:-1]) + " and " + names[-1] + "!"
print(greeting)

Output

Hi, John, Jane and Bob!

Using the + operator and string formatting

Example

In this example, we use string formatting to insert the name, age, and height variables into a message string. We use curly braces {} as placeholders for the variables and call the format() method on the message string with the variables as arguments. The resulting string is "My name is Alice and I'm 25 years old. My height is 1.65 meters.". Note that we use :.2f to format the height variable as a floating point number with two decimal places.

name = "Alice"
age = 25
height = 1.65
message = "My name is {} and I'm {} years old. My height is {:.2f} meters.".format(name, age, height)
print(message)

Output

My name is Alice and I'm 25 years old. My height is 1.65 meters.

Updated on: 08-May-2023

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements