You are currently viewing Python String Concatenation

How to concatenate strings in Python? Concatenation is the process of joining two or more items into a single item, and when it comes to strings, string concatenation is the process of combining multiple strings into a single string. The + operator can be used to concatenate strings together. When you use the + operator with string operands, it combines the strings and returns a new string that contains the concatenated result.

Advertisements

You can concatenate strings in Python using many ways, for example, by using + operator, join(), format(), % operator, f-string, and “,” comma. In this article, I will explain string concatenation by using all these functions with examples.

1. Quick Examples of Concatenating the Strings

If you are in a hurry, below are some quick examples of how to concatenate strings.


# Quick examples of string concatenation

# Defining strings
string1 = "Welcome To "
string2 = "SparkByExamples"

# Example 1: Using the + operator
# String concatenation 
result = string1 + string2

# Example 2: Using join() method
# The string with a separator Space(" ")
result = " ".join([string1,string2])

# Example 3: Using the join() method 
# With a separator of space " "
string_list = ["Welcome", "To", "SparkByExamples"]
result = " ".join(string_list)

# Example 4: Using format() function
# String concatenation 
result = "{} {}" .format(string1, string2) 

# Example 5: Using named placeholders with format() for string concatenation
result = "{first_part} {second_part}".format(first_part=string1, second_part=string2)

# Example 6: Using % operator 
# For string concatenation
result = "%s %s" % (string1, string2)

# Example 7: Using “, ” comma
print(string1, string2)

# Example 8: Using f-string
# String concatenation
result = f"{string1} {string2}"

2. String Concatenation Using the + Operator

In Python, you can perform string concatenation using the + operator. This operator combines two strings, named string1 and string2, to produce a new string called result.

The below example string1 contains the text "Welcome To " and string2 contains the text "SparkByExamples". When you use the + operator to concatenate these two strings, the result is “Welcome To SparkByExamples”, and it is stored in the variable result.


# Defining the strings
string1 = "Welcome To "
string2 = "SparkByExamples"

# Using the + operator to
# concatenate the strings 
result = string1 + string2
print("First string:", string1)
print("Second string:", string2)
print("After concatenating the string.:",result)

Yields below output.

python string concatenation

Remember that the + operator should only be used for a small number of concatenations because it creates new string objects each time it’s used. For a larger number of concatenations, using str.join() a list to build the final string is more efficient, as it reduces the overhead of creating multiple new string objects.

3. String Concatenation Using join() Method

String concatenation using the join() method is a more efficient way to concatenate a large number of strings. The join() method belongs to the string class and allows you to concatenate elements of an iterable (e.g., a list, tuple, or set) with a specified separator between each element.

You can use the join() method to concatenate two strings with a separator of a single space (""). For example, this method is applied to a list [string1, string2], with a space "" as the separator. It joins the elements of the list together, placing the separator ("") between each element. As a result, the concatenated string is "Welcome To SparkByExamples", which is stored in the variable result.


# Defining strings
string1 = "Welcome To "
string2 = "SparkByExamples"
 
# Using join() method
# The string with a separator Space(" ")
print("First string:", string1)
print("Second string:", string2)
result = " ".join([string1,string2])
print("After concatenating the string:",result)

Yields the same output as above.

Another way to concatenate the strings using join() method. This method joins the elements of the list of strings(string_list) into a single string with a space "" as the separator between each element.

In the below example, the join() method is applied to the list(string_list), with a space "" as the separator. This method joins the elements of the list together, placing the separator (” “) between each element. As a result, the concatenated string is “Welcome To SparkByExamples”, which is stored in the variable result.


# Defining list of strings
string_list = ["Welcome", "To", "SparkByExamples"]

# Using the join() method 
# With a separator of space " "
result = " ".join(string_list)
print("After concatenating the string:", result)

Yields the same output as above.

4. String Concatenate Using format() Function

You can also use the format() function to concatenate the string. It joins the two strings, string1 and string2, using the positional formatting provided in the format string.

In the below example, the format string “{} {}” contains two curly braces {} as placeholders for the variables to be inserted. When the format() the function is called, it replaces the first {} with the value of string1 and the second {} with the value of string2. As a result, the concatenated string is “Welcome To SparkByExamples”, which is stored in the variable result.


# Defining strings
string1 = "Welcome To "
string2 = "SparkByExamples"

# Using format() function
# String concatenation 
result = "{} {}" .format(string1, string2) 
print("After concatenating the string:", result)

Yields the same output as above.

You can also use named placeholders with the format() function for explicit substitutions. In this program, you used named placeholders {first_part} and {second_part}, and provided the values for these placeholders explicitly using named arguments in the format() method.


# Using named placeholders with format() for string concatenation
result = "{first_part} {second_part}".format(first_part=string1, second_part=string2)
print("After concatenating the string:", result)

Yields the same output as above.

5. String Concatenate Using % Operator

You can also use the % operator for string formatting, including string concatenation. This operator is commonly known as the “string formatting operator” or “string interpolation operator.” It allows you to insert values into a string using placeholders that start with %. It concatenates the strings string1 string2 with a space "" as the separator.

In the below example, the format string "%s %s" contains two placeholders %s, and the % operator is used to perform the string concatenation. The tuple (string1, string2) contains the values that will be inserted into the placeholders in the order they appear.


# Defining strings
string1 = "Welcome To"
string2 = "SparkByExamples"

# Using % operator 
# For string concatenation
result = "%s %s" % (string1, string2)
print("After concatenating the string:", result)

Yields the same output as above.

6. String Concatenate Using “, ” Comma

You can also use the print() function with the ',' (comma) separator to concatenate strings during output. When you pass multiple strings as arguments to the print() function, they are automatically concatenated with a space as the default separator.

In the below example, you used the print() function with the ',' separator to output the two strings string1 and string2. When you use the ',' separator with print(), Python automatically adds a space between the two strings (as well as after the colon), resulting in the concatenated output.


# Defining strings
string1 = "Welcome To"
string2 = "SparkByExamples"

# Using “, ” comma
print("After concatenating the string:", string1, string2)

Yields the same output as above.

7. String Concatenation Using f-string

F-strings provide a concise and efficient way to concatenate strings in Python, making string formatting and interpolation more readable and straightforward. Using f-strings for string concatenation. It effectively joins the two strings string1 and string2 with a space in between.

In this program, you used an f-string with the f prefix at the beginning of the string. Inside the f-string, you included {string1} and {string2} within curly braces {}. Python automatically evaluated the expressions inside the curly braces and substituted them with the values of string1 and string2, respectively.


# Defining strings
string1 = "Welcome To"
string2 = "SparkByExamples"

# Using f-string
# String concatenation
result = f"{string1} {string2}"
print("After concatenating the string:", result)

Yields the same output as above.

Conclusion

In this article, I have explained how to concatenate strings in Python by using the + operator, join(), format(), % operator, f-string, and “,” comma with examples.

Happy Learning !!