Python Join() Method with Examples

The python join method is a string method that returns a string in which the elements of the strings are joined to a string separator. The python string join method is particularly useful when you want to concatenate the elements of an iterable object to a separator. The join method allows you to form strings from objects that are iterable.

Signature: string.join(iterable, /)
Docstring:
Concatenate any number of strings.

The string whose method is called is inserted in between each given string.
The result is returned as a new string.

Parameters for the Join Method

The python join method takes one vital parameter, an iterable which is basically an object that is capable of returning its item members one after the other. Some examples of an iterable include a string, list, tuple, dictionary, or set. An iterable could as well be file objects or any object that is defined with the __getitem()__ method or an __iter__() method. 

The Return Value from the Python Join Method. 

The Python join method basically returns a string of the elements of the iterable concatenated by the string separator. It is important to note that the separator must be a string. If in any other datatype, say an integer, floating-point number, or a Boolean, the join method raises a TypeError exception. 

Let’s now see some examples of the join method in the most common use cases. 

Example 1: Using the Python string join method on a String

#define a string
string = 'PythonTutorial'
#define the separator, spacebar
sep = ' '

#use the join method to add the separator after each character of the string
joined_result = sep.join(string)

#print the joined result
print(f"The joined result of sep.join(string) is: {joined_result}")

Output:

The joined result of sep.join(string) is: P y t h o n T u t o r i a l

Example 2: Using the Join Method on a List

#define a list
the_list = ['Python', 'is', 'one', 'of', 'the', 'most', 'popular', 'programming', 'languages']
#define the separator,!
sep = '! '

#use the join method to add the separator after each character of the list
joined_result = sep.join(the_list)

#print the joined result
print(f"The joined result of sep.join(the_list) is: {joined_result}")

Output:

The joined result of sep.join(string) is: Python! is! one! of! the! most! popular! programming! Languages

Example 3: Using the Join Method on a Tuple

#define a tuple
the_tuple = ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10')
#define the separator,#
sep = '# '

#use the join method to add the separator after each character of the string
joined_result = sep.join(the_tuple)

#print the joined result
print(f"The joined result of sep.join(the_tuple) is: {joined_result}")

A tuple is almost like a list except that the elements of a tuple are immutable, i.e. they cannot be changed. In this example, the iterable will be in the form of a tuple. 

Output:

The joined result of sep.join(the_tuple) is: 1# 2# 3# 4# 5# 6# 7# 8# 9# 10

Example 4: The Python Join Method with Sets

#define a set
the_set = {'HTML', 'CSS', 'PHP', 'Javascript'}

#define the separator, >>
sep = '>> '

#use the join method to add the separator after each element of the set
joined_result = sep.join(the_set)

#print the joined result
print(f"The joined result of sep.join(the_tuple) is: {joined_result}")

Output:

The joined result of sep.join(the_tuple) is: Javascript>> CSS>> HTML>> PHP

Note that the arrangement of the element changed in the output. This is because a set is a collection of items in an unordered manner. Here, the other is randomly selected. 

Example 5: The Join Method with Dictionaries

The join method can be used in a dictionary as well. Note that the merging is done on the keys of the dictionary and not the values of the dictionary. Let’s take an example to further explain this.

#define a dictionary
the_dictionary = {'HTML': 1, 'CSS': 2, 'PHP': 3, 'Javascript': 4}

#define the separator, >>
sep = '>> '

#use the join method to add the separator after each element of the dictionary keys
joined_result = sep.join(the_dictionary)

#print the joined result
print(f"The joined result of sep.join(the_dictionary) is: {joined_result}")

 Output:

The joined result of sep.join(the_dictionary) is: HTML>> CSS>> PHP>> Javascript

If the dictionary keys and values are flipped such that the dictionary keys are not strings, the program will throw an error. Take a look. 

#define a dictionary
the_dictionary = {1: 'HTML', 2: 'CSS', 3: 'PHP', 4: 'Javascript'}

#define the separator, >>
sep = '>> '

#use the join method to add the separator after each character of the dictionary keys
joined_result = sep.join(the_dictionary)

#print the joined result
print(f"The joined result of sep.join(the_dictionary) is: {joined_result}")

Output: 

TypeError                                 Traceback (most recent call last)
<ipython-input-24-451dcd2e5dee> in <module>
      6 
      7 #use the join method to add the separator after each character of the string
----> 8 joined_result = sep.join(the_dictionary)
      9 
     10 #print the joined result
TypeError: sequence item 0: expected str instance, int found

In this tutorial, we have discussed how to use the python join() method with examples. You can now go ahead and try it out yourself. 

Leave a Comment

Pin It on Pinterest