Open In App

Python String join() Method

Last Updated : 12 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The join() method in Python is used to concatenate the elements of an iterable (such as a list, tuple, or set) into a single string with a specified delimiter placed between each element.

Lets take a simple example to join list of string using join() method.

Joining a List of Strings

In below example, we use join() method to combine a list of strings into a single string with each string separated by a space.

a = ['Hello', 'world', 'from', 'Python']
res = ' '.join(a)
print(res)  

Output
Hello world from Python

Syntax of join()

separator.join(iterable)

Parameters:

  • separator: The string placed between elements of the iterable.
  • iterable: A sequence of strings (e.g., list, tuple etc) to join together.

Return Value:

  • Returns a single string formed by joining all elements in the iterable, separated by the specified separator
  • If the iterable contains any non-string values and it raises a TypeError exception.

Examples of join() Method on Different Data Types

Below are examples of how join() method works with different data types.

Using join() with Tuples

The join() method works with any iterable containing strings, including tuples.

s = ("Learn", "to", "code")

# Separator "-" is used to join strings
res = "-".join(s)
print(res)

Output
Learn-to-code

Using join() with set

In this example, we are joining set of String.

s = {'Python', 'is', 'fun'}

# Separator "-" is used to join strings
res = '-'.join(s)
print(res) 

Output
Python-is-fun

Note: Since sets are unordered, the resulting string may appear in any order, such as “fun is Python” or “Python is fun”.

Using join() with Dictionary

When using the join() method with a dictionary, it will only join the keys, not the values. This is because join() operates on iterables of strings and the default iteration over a dictionary returns its keys.

d = {'Geek': 1, 'for': 2, 'Geeks': 3}

# Separator "_" is used to join keys into a single string
res = '_'.join(d)

print(res)

Output
Geek_for_Geeks

Frequently Asked Questions on Python String Join() Method

What does join() method do in Python?

The join() method takes an iterable (such as a list, tuple, set, or dictionary) and joins its elements into a single string using a specified delimiter.

Can the join() method join non-string elements?

No, all elements in the iterable must be strings. If the iterable contains non-string elements, they need to be converted to strings first using the str() function.

What happens when you use join() with a dictionary?

By default, the join() method joins only the keys of the dictionary.

Can I use join() with nested lists or tuples?

The join() method only works with a flat iterable containing strings. For nested lists or tuples, you need to flatten the structure or handle each inner element separately

What happens if the iterable is empty?

If the iterable is empty, the join() method returns an empty string without raising any error.



Next Article

Similar Reads

three90RightbarBannerImg