| by Arround The Web | No comments

Python String Concatenation Examples

 In Python, strings are one of the most fundamental and versatile data types. They are sequences of characters that can represent text, symbols, or even binary data. In contrast to some other languages, Python does not have separate data types for single characters. Instead, a character is just a string of length one. The “Square Bracket []” is used to access individual characters/substrings within a string.

In this article, we will present you with a comprehensive guide on joining/combining strings in Python via the below contents:

What is String Concatenation in Python?
How to Perform String Concatenation in Python?

Conclusion

What is String Concatenation in Python?

String concatenation is the process of combining/joining two or more strings to create a single new string. For example, if you want to join the strings “Hello” and “World” with concatenation, you get the new string “Hello World”. You can use this method to combine different information pieces into one string.

How to Perform String Concatenation in Python?

In Python, there are several ways to concatenate or combine strings, but the simplest one is using the “+” operator. We will discuss all of these methods in detail with example codes. Let’s start with the most basic string concatenation method:

Performing String Concatenation Using “+” Operator

The “+” operator takes two strings as operands and retrieves the new string which is the result of appending the second string to the end of the first string. Let’s take this code as an example, we concatenate two strings with the “+” operator and retrieve a new string:

string_1 = "Hello "
string_2 = "World"
# Using + Operator
print(string_1 + string_2)

 
The below output shows the concatenation string:


We can also perform concatenation on more than two values. For example, take this code, here, we initialize multiple string values to a specified variable. The string concatenation operator “+” is used to concatenate/combine all four given strings and retrieve the new concatenated string.

string_1 = "Hello "
string_2 = "World."
string_3 = " Welcome to"
string_4 = " Python Guide"
# Using + Operator
print(string_1 + string_2 + string_3 + string_4)

 
The output will be as follows:


Performing String Concatenation Using “%” Operator

The string formatting method “%” operator can also be used for string concatenation. It is used to concatenate multiple strings by performing string formatting in Python. Here in the below code, we initialize the two strings and concatenate them using the “%” operator. The “%s” specify the string data type which is the placeholder for the string variable passed inside the parentheses.

string_1 = "Hello "
string_2 = "World"
# Using % Operator
print("%s %s" %(string_1, string_2))

 
The string concatenated successfully:


Performing String Concatenation Using “join()” Method

The “join()” method is used to join/combine the sequence elements by a string operator and retrieve a new string. In the below code, we combine the string variable “string_1” and “string_2” using the join() method. We passed the two string variables as a list to the “join()” method parameter.

Note: The join() method takes only the sequence as its argument, if we pass the two string variables separately it will retrieve an error.

Perform this method by following the code:

string_1 = "Hello"
string_2 = "World"
# Using join() Method
print("".join([string_1, string_2]))
# Using join() Method With a Separator
print("-".join([string_1, string_2]))

 
The below output shows the string with separator and without separator:


Performing String Concatenation Using “format()” Method

In Python, the “string.format()” method is also a string formatting method. It is used to substitute the argument value of the method to the placeholder “{}” of the specified string. We can also employ this method to concatenate two strings. In the below code, the “format()” method concatenates two given strings and retrieves them to the new string:

string_1 = "Hello"
string_2 = "World"
# Using format() Method
print("{} {}".format(string_1, string_2))

 
The concatenated strings will be shown below:


Performing String Concatenation Using “format_map()” Method

The “format_map()” method is similar to the “format()” method used for formatting strings. We can use the “format_map()” to concatenate multiple strings in Python. The following example will initialize the dictionary with two key-value pairs. Next, the “format_map()” method takes the dictionary as an argument and concatenates the key value by using the placeholder “{x}”, and “{y}” inside the new string.

dict1 = {'x':'Hello', 'y': 'World'}
# Using format_map() Method
print("{x} {y}".format_map(dict1))

 
The output will be as follows:


Performing String Concatenation Using the “f-string” Method

In Python version “3.6”, a new string method “f-string” is introduced. This method is the formatting method that is used to format the string in a straightforward way. We can employ this method to combine/join the multiple strings into new strings. Take the following example code to do this:

string_1 = "Hello"
string_2 = "World"
# Using f-string Method
print(f"{string_1} {string_2}")

 
The output will look like this:


Performing String Concatenation Using “,” Comma

The “comma (,)” can also be used for string concatenation in Python. It is an alternative to the “+” string concatenation operator. We can use this method in the below example to combine the two input string variables “string_1” and “string_2”:

string_1 = "Hello"
string_2 = "World"
# Using {,} Comma
print(string_1, string_2)

 
The above code retrieves this:


Performing String Concatenation Using “*” Operator

The asterisk (*) operator can be used to concatenate the same string repeatedly. We can only concatenate the same string, but cannot concatenate with other strings. In the following code block, we concatenate the same string three times using the asterisk operator:

string_1 = "Hello "
# Using * operator
print(string_1*3)

 
The below code shows this:


Performing String Concatenation Using “+=” Operator

Like the “+” operator, we can also use the “+=” operator for string concatenation. This method modifies the original string and concatenates the new string. The below code demonstrates this method:

string_1 = "Hello "
string_1 += "World"
# Using += Operator
print(string_1)

 
The output will look like this:


Performing String Concatenation Using “StringIO”

The “StringIO” module provides a file-like object that can read and write the strings. We can also use this module for string concatenation in Python. To perform string concatenation, first, we need to import the StringIO module from the io module. Next, we create the StringIO object named “obj”. After that, we write the strings to the object that we want to concatenate using the “write()” method. At last, we get the concatenated string using the “getvalue()” method. Use the below code to concatenate strings in Python:

from io import StringIO
obj = StringIO()
obj.write("Python")
obj.write(" Guide")
print(obj.getvalue())

 
The input strings have been concatenated successfully:

Conclusion

The “+” operator, “%” operator, “join()” method, “format()” method, “format_map()” method, and other methods are used to concatenate strings in Python. These methods can combine/concatenate two or more than two strings in Python. Some of the other methods such as the “f-string” method, “,” comma, “*” operator, “+=” operator, and “StringIO” module can be used to concatenate two strings in Python. This guide delivered all methods for string concatenation in Python.

Share Button

Source: linuxhint.com

Leave a Reply