Join Regular Classroom : Visit ClassroomTech

join() in Python String

It is a method in python that takes all the iterable and joins them into one string by a separator. It returns a String. It is operational on lists, tuples, dictionaries and every iterables.

Syntax:

separator.join(iterable)

Example 1:

s = [“code“, “codewindow“, “hustle“]
x = ‘#‘.join(s)
print(x)

Output:

code#codewindow#hustle

Explanation:

Here the iterables in the list are converted to a single string using “#” as the separator.

Example 2:

s = (“code“, “codewindow“, “hustle“)
x = ‘, ‘.join(s)
print(x)

Output:

code, codewindow, hustle

Explanation:

Here all the iterables in the tuple are converted to a single string using “, ” as the separator.

Example 3:

s = {“Name“:”codewindow“, “Country“:”India“}
x = ‘, ‘.join(s)
print(x)

Output:

Name, Country

Explanation:

Here it joins all the keys of the dictionary into a single string separated by “, ”.

Note: When using a dictionary as an iterable it reruns the keys, not the values.

Example 4:

s = {“Name“:”codewindow“, “Country“:”India“}
x = ‘, ‘.join(s.values())
print(x)

Output:

codewindow, India

Explanation:

Here it joins all the values of the dictionary into a single string separated by “, ”.

You Missed
Also Checkout