Home » Python String | join() method with Examples

Python String | join() method with Examples

by Online Tutorials Library

Python String join() Method

Python join() method is used to concat a string with iterable object. It returns a new string which is the concatenation of the strings in iterable. It throws an exception TypeError if iterable contains any non-string value.

It allows various iterables like: List, Tuple, String etc.

Signature

Parameters

iterable : iterable object like: List, Tuple, String etc.

Return

It returns a new string or an exception TypeError if iterable contains any non-string value.

Let’s see some examples of join() method to understand it’s functionalities.

Python String join() Method Example 1

A simple example which implements join() method with the List iterable, see the example below.

Output:

1:2:3  

Python String join() Method Example 2

A list iterable join with empty string and produce a new string, see the example below

Output:

Tutoraspire  

Python String join() Method Example 3

An example of join() method with Set iterable. Set contains unordered elements and produce different output each times. See the example below.

Output:

Java->Python->C#  

Python String join() Method Example 4

In case of dictionary, this method join keys only. Make sure keys are string, otherwise it throws an exception.

Output:

key1&key2  

Next TopicPython Strings

You may also like