Python String join Method

Python join(seq)

Python String join Method

The join method is a fundamental technique within Python that is associated with the string class. This method is utilized to concatenate or merge elements from an iterable—such as a list, tuple, or string—into one cohesive string.

Syntax of Python String join Method

The join method utilizes the following syntax.

Example

String.join(iterable)

Example: Using String Join Method

Let’s examine a case to demonstrate how the join method is utilized:

Example

lis = ['Hello', 'World', 'Python', 'Programming']

r = ' '.join(lis)

print(r)

Output:

Output

Hello World Python Programming

Explanation:

In this example, the string "," consisting solely of a single space character serves as the foundation for invoking the join method. The iterable named lis comprises four components. By utilizing the join method with the separator "", the string "Hello World Python Programming" is formed by merging the elements from lis. Finally, this string is presented as output.

It supports a range of iterables, including: List, Tuple, String, and more.

Signature:

It has the following signature:

Example

join(iterable)

Parameters

  • iterable: an iterable object such as a List, Tuple, String, etc.
  • Return

It produces a new string or raises a TypeError exception if the iterable includes any values that are not strings.

Different Examples for Python String join Method

Let’s explore a few examples of the join method to comprehend its functionalities more thoroughly.

Example 1

Here is a straightforward illustration that demonstrates the use of the join method in conjunction with the List iterable. Please refer to the example provided below.

Example

# Python join() method example

# Variable declaration

str = ":"   # string

list = ['1','2','3']    # iterable

# Calling function

s2 = str.join(list)

# Displaying result

print(s2)

Output:

Example 2

An iterable list can be concatenated using an empty string, resulting in the creation of a new string. Refer to the example below for clarification.

Example

# Python join() method example

# Variable declaration

S1 = ""   # string

list = ['J','a','v','a','t','p','o','i','n','t']    # iterable

# Calling function

S2 = str.join(list)

# Displaying result

print(S2)

Output:

Output

Example

Example 3

An illustration of the join function utilizing a Set iterable. Since a Set comprises unordered items, it generates varied results each time it is executed. Refer to the example provided below.

Example

# Python join() method example

# Variable declaration

str = "->"   # string

list = {'Python','C#','Python'}    # iterable

# Calling function

s2 = str.join(list)

# Displaying result

print(s2)

Output:

Output

Java->Python->C#

Example 4

When dealing with a dictionary, this approach exclusively combines the keys. It is essential to ensure that the keys are of string type; otherwise, an exception will be raised.

Example

# Python join() method example

# Variable declaration

dic =  {'key1': 1, 'key2': 2}

str = '&'

# Calling function

str = str.join(dic)

# Displaying result

print(str)

Output:

Output

key1&key2

Input Required

This code uses input(). Please provide values below: