Hit enter after type your search item

What is Python join method?

The Python join method concatenates the string of a given sequence. The sequence can be lists, tuple, range etc. The elements of a sequence are joined by a given separator e.g. by space, comma, tab etc.

I will show you the examples of using join in these iterables, first have a look at the syntax of using the join method.

Join with List Example Set Example Tuple Example

How to use the join method?

The general syntax of using the join method is:

str.join(iterable)

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:

See online demo and code


Python join

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:

See online demo and code


join new line

How to use 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:

See online demo and code


join tuple

You can see, 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:

See online demo and code


join set

What if a sequence contains integers or float?

As such, different sequences may contain the 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 join method is used:

See online demo and code


join TypeError

You can see, out 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 join method can work. See the examples with code snippets by following the links below:

Example1 Example2

For that, I have written a detailed guide that covers three ways of dealing with this situation.

This div height required for enabling the sticky sidebar