Python String join() Method
Last Updated :
27 Oct, 2024
In this article, we are going to explore the Python string join() method and discuss the join method on lists, tuples, sets and dictionaries. Lets take a quick example for joining list of string using join().
Joining a List of Strings
In the example below, we use join() method to combine a list of strings into a single string, with each string separated by a space.
Python
a = ['Hello', 'world', 'from', 'Python']
res = ' '.join(a)
print(res)
OutputHello world from Python
What is join() Method?
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.
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, it raises a TypeError exception.
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.
Python
s = ("Learn", "to", "code")
# Separator "-" is used to join strings
res = "-".join(s)
print(res)
Using join() with set
In this example, we are joining set of String.
Python
s = {'Python', 'is', 'fun'}
# Separator "-" is used to join strings
res = '-'.join(s)
print(res)
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.
Python
d = {'Geek': 1, 'for': 2, 'Geeks': 3}
# Separator "_" is used to join keys into a single string
res = '_'.join(d)
print(res)
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.