Python String Join Method Tutorial

Python String Join() Method

This is a detailed tutorial of the Python String Join Method. Learn to convert a Python Iterable Object into a String using some separator or delimiter.

Python String join()

The join() method can be applied to any of the Python String Objects. It simply converts an Iterable object into the string by using the string itself on which it is applied as the delimiter for separating each element of the iterable object in the consequent string. In other words, you can also think of the join() function as a concatenation function as it concatenates the various items of the iterable using any string as the separator symbol.

Syntax

join() Parameters

The join() method only takes a single argument and that is the iterable object that is to be concatenated. These iterables should be able to return one of its items at a time. List, Tuple, Dictionary, String, and Sets are examples of some of the Native Iterable Python Data Types.

Apart from the native data types, you can also make your own created python class objects to return one object at a time by defining the __iter__() and __getitem()__ methods inside the class. These methods will make your own class objects iterable objects.

join() Return Type

The return type of the method join() is obviously the concatenated string. It will return a valid concatenated string if and only if the provided iterable would contain all of its items that will be belonging to the string data type itself. If the provided iterable has one or more items that do not belong to the string data type, it will raise the TypeError Exception.

Examples

The various examples illustrating the use of join() method for different iterables are given below.

Example 1. Working of Join() Method For List and Tuples

This example illustrates the use of the join() method for concatenating the Python List and Tuple Iterables.

In the above code, we’re first concatenating a Python List using different separator strings. Then we’re also concatenating a Tuple using the join() method.

Python String Join() Method Example

Example 2. Python String join() Method For Set Iterables

For Set Iterables, the join() method works in a slightly different way. The Set is a collection of items and it is unordered. As the items are not always in the same order, the returned output of the join() method is also not always the same. Therefore, you will get a different concatenated string for the set iterable with the join() method applied.

Instead of a single output, I executed the above program twice and given their different outputs below. This is to illustrate that as the set order is unordered, the concatenated string is also in a different order every time.

Python String Join() Method For Set Example 1
First Execution
Python String Join() Method On Set Example 2
Second Execution

Example 3. Join() Method Applied For Python Dictionaries

As you can observe in the output, the join() method when provided a Python Dictionary as an argument, it concatenates the Keys of the dictionary and not the values.

Python String Join() Method For Dictionaries Example

Example 4. Non-String Data Type Items For Python String Method

When the iterable provided to the join() method as an argument contains non-String data type elements, it raises TypeError and this is illustrated in the following example for a List, a Tuple, a Set, and a Dictionary as well.

I’ve used the Try Except Block to print down the excepts for all of the iterables.

Python String Join() Method TypeError Exception Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *