What is Python join() method?
- The Python join() method concatenates the string of a given sequence.
- The sequence can be lists, tuples, ranges, etc.
- The elements of a sequence are joined by a given separator e.g. by space, comma, tab, etc.
I will show you examples of using join in these iterables, first have a look at the syntax of using the join method.
How to use the join method?
The general syntax of using the join method is:
Where:
- Str is a separator by which string objects in iterable will be concatenated
- Iterable is a sequence e.g. a list, tuple etc.
- If the iterable contains a non-string element like an integer or float then a TypeError will be raised. It also includes bytes objects.
An example of using join method with a list
In this example, a list of string objects is created. The list contains four string elements. After that, the Python join method is used with the comma separator. See the code and output:
#A demo of join method lst_join = ['Python', 'String', 'Join', 'Method'] str_join = " ".join(lst_join) print ("The string after join: ", str_join)
Output:
A Demo of using \n separator
In this example, the same list is used as in above example except the \n is used as a separator. This will add a new line and see how it will display the resultant string:
#A demo of join method with new line lst_join = ['Python', 'String', 'Join', 'Method'] str_join = "\n".join(lst_join) print (str_join)
Output:
String
Join
Method
How to use the join() method with a tuple?
As mentioned earlier, you may use the join method to concatenate sequence items into strings. The tuple is another sequence, so you may convert a tuple into the string as well.
In the following example, the tuple items are joined by “ + “ separator and displayed by using the print function:
#A demo of join method with Tuple tup_join = ('Tuple', 'Example', 'With', 'Join') str_join = " + ".join(tup_join) print (str_join)
Output:
You can see that the join method returned a string concatenated with space, plus, and again a space character.
An example of using Python join method with a set
Similarly, you may use the join Python method with the set. The set is created by using the curly braces and items are separated by a comma.
In this example, a set of four objects is created. Then, the join method is used with the “ , “ separator i.e. a space, comma and a space. Finally, the returned string after the join method is displayed. Have a look:
#A demo of join method with Set set_join = {'Shakeera','Mike','Shoaib', 'Tania','Samina'} str_join = " , ".join(set_join) print ("The string after join: ",str_join)
Output:
What if a sequence contains integers or float?
As such, different sequences may contain integers and float objects along with strings. So, what will happen if you use the join method on a list or tuple or set and it contains mixed objects?
See what happens in this example where a list of mixed objects is created and the join method is used:
#A demo of join method with mixed list lst_join_mixed = ['The', 'String', 'Join', 10] str_join_mixed = " ".join(lst_join_mixed) print ("The string after join: ", str_join_mixed)
You can see that out our list contains an integer object. As this code is executed, a TypeError occurred and it says int found. Similarly, if a list contains a float, the TypeError will occur with “float found” message.
So, how to handle this? Keep reading.
Using join method with mixed sequence
If your sequence (list, tuple, set, etc.) contains mixed objects i.e. strings, float, and integers then you have to convert those integers and float to string before the join method can work.
For that, we have written a detailed guide that covers three ways of dealing with this situation.