| by Arround The Web | No comments

Python String Join() Method

Sometimes, we need to join multiple strings or elements into one string in programming. For example, we may want to create sentences, file paths, data formats, or other combinations of elements. To accomplish this, Python delivers a built-in method named “string.join()”. This method takes an iterable of strings or elements and retrieves a particular string that is the joining of them. We can also specify the separator that is inserted between each item in the sequence/iterable.

This article discusses the below contents in detail utilizing multiple examples:

What is the Python “string.join()” Method?

Conclusion

What is the Python “string.join()” Method?

In Python, the “join()” method is a standard method that is utilized to join the items in a sequence/iterable into a single string. The “string.join()” method accepts a sequence such as a tuple, list, or string as its parameter. This method is an efficient and suitable way to join items/elements without utilizing explicit loops or string concatenation operators.

Syntax

string.join(iterable)

 
Parameters

In the above syntax, the “iterable” parameter specifies any sequence/collection of items that we want to join/merge. While the “string” indicates the separator string that will be utilized to join the iterable element.

Return Value

The “string.join()” method retrieves the new string where the sequence items are joined utilizing the particular string as a separator.

Example 1: Joining Tuple Elements into a String Utilizing Specified Separator

In this example, the tuple containing the string element is initialized. After that, the “string.join()” method is used to join all the elements/items of the input tuple to a new string with a particular separator “-” value.

tuple1 = ("Joseph", "Anna", "Lily")
print("-".join(tuple1))

 
The above code displays the following string to output:


Example 2: Joining Dictionary Elements Into a String Utilizing Specified Separator

We can also join/concatenate the dictionary elements into a specified string with a specified separator value. Here, we join the dictionary key value with the separator “,” to a new string using the “string.join()” method:

dict1 = {"Name": "Joseph", "Age": 25}
print(",".join(dict1))

 
The following snippet displays the new string:


Note: If you iterate over a dictionary, you will get the keys as the output, not the values.

If the dictionary keys are not a string, then the “TypeError” is raised to the output:

dict1 = {0: "Joseph", 1: "Anna"}
print(",".join(dict1))

 
The above code displays this output:


To solve this TypeError, we need to convert all the elements/items of the dictionary into a string. For example, here, we utilize the “str()” method with the for loop to iterate over a dictionary and convert each key of the dictionary into a string. Take this code as an example:

dict1 = {0: "Joseph", 1: "Anna"}
print(",".join(str(d) for d in dict1))

 
The above code successfully joined the integer dictionary keys element with the specific separator value:


Example 3: Joining Sets Elements Into a String Utilizing Specified Separators

The set elements can also be joined into a string by using the specified separators. For example, in this code, the string.join() method joins the elements of the set to the new string with “, ” separator and with “->->” separator.

sets1 = {'Ten', 'Five', 'Six'}
print(', '.join(sets1))

sets2 = {'Python', 'Java', 'C++'}
print('->->'.join(sets2))

 
The following output shows the string.join() method:


Example 4: Joining One String Into Another String Utilizing Each of Them as a Separator

In the below code, we initialize two strings named “string1” and “string2”. After that, the string.join() method is used to join one string to another with the second string as the separator value. For example, in the first join each item of string2 is separated by the string1 elements. While in the second joining each element of string1 is separated by string2 elements.

string1 = 'Jon'
string2 = '25'
print(string1.join(string2))
print(string2.join(string1))

 
The execution of the above code displays this:


Example 5: Separating List Elements Using the “string.join()” Method With Specific Separator

In the below code, we first created a list of strings with some empty strings. Next, the “string.join()” method is used to concatenate the elements of list1 that are not empty with specified separators. Lastly, the resulting string is displayed to the console.

list1 = ["Joseph", "Anna", "", "Lily", ""]
output = "@ ".join(ele for ele in list1 if ele)
print(output)

 
Here is the output:


Example 6: Joining List to String With Unicode Character Separator Value

We can also concatenate the list to a string with a Unicode character value. Here, in the code below, the list and the Unicode separator are initialized to a specified variable. Next, the “string.join()” method joins the list of elements/items to a string with a Unicode character as a separator.

list1 = ['Joseph', 'Anna', 'Lily']
sep = ' õ '
print(sep.join(list1))

 
The following string has been retrieved to the output:

Conclusion

The “string.join()” method of Python is utilized to join/concatenate any given sequences or iterable elements into a string with a custom separator value. We can concatenate any sequence such as list, tuple, sets, etc, and also define any separator value such as comma, symbols, string, etc. This Python write-up delivered various examples of the Python “string.join()” method.

Share Button

Source: linuxhint.com

Leave a Reply