string join function in python

Python String Join – With Examples

In python, the string join method, join(), is used to combine all the items of an iterable (example – list, tuple, set, etc.) into a single string separated by a separator.

string join function in python
ls = ["I", "like", "coding", "in", "python"]
print(" ".join(ls))

Output:

I like coding in python

  • Syntax
  • Examples

The following syntax is used to apply the string join function in python.

string.join(iterable)

Note: The string in the above syntax refers to the string separator to use for joining the items in the iterable.

Parameters:

The join() function takes an iterable as its parameter. Iterable are objects capable of returning its member one at a time. List, tuple, set, dictionary, string, etc are all iterable objects.

Returns:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

A string resulting from the concatenation of the elements of the iterator and separated by the separator string passed.

Note: A TypeError exception is raised if the iterator contains any non-string values.

Example 1: Joining elements of a list

ls = ['a', 'b', 'c', 'd']
print("The iterable to join:", ls)
# Using a single space as a separator
print("Join using single space:", " ".join(ls))
# Using an empty string as a separator
print("Join using empty string:", "".join(ls))

Output:

The iterable to join: ['a', 'b', 'c', 'd']
Join using single space: a b c d
Join using empty string: abcd

In the above example, we see that the elements of the list are joined using the separator string provided.

Example 2: When the iterable contains a non-string value

ls = ['a', 1, 'b', 'c', 'd']
# Using a single space as a separator
print("Join using single space:", " ".join(ls))

Output:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-5f89fa28c958> in <module>
      1 ls = ['a', 1, 'b', 'c', 'd']
      2 # Using a single space as a separator
----> 3 print("Join using single space:", " ".join(ls))

TypeError: sequence item 1: expected str instance, int found

In the above example, a TypeError is raised as the iterable contained a non-string value.

Example 3: Joining elements of a tuple

tup = ('a', 'b', 'c', 'd')
print("The iterable to join:", tup)
# Using a single space as a separator
print("Join using single space:", " ".join(tup))
# Using an empty string as a separator
print("Join using underscore:", "_".join(tup))

Output:

The iterable to join: ('a', 'b', 'c', 'd')
Join using single space: a b c d
Join using underscore: a_b_c_d

Tuples are also iterable and hence the join function is able to concatenate its element into a single string using the separator provided.

Example 4: Joining elements of a set

set1 = {'a', 'b', 'c', 'd'}
# Using a single space as a separator
print("Join using comma:", ",".join(set1))

Output:

Join using comma: a,b,d,c

In the above example, the elements of the set have been joined using the separator , but the order is not the same as in the set we initialized. This is because a set is an unordered collection so you may get different sequences in output when working with sets.

Example 5: Joining elements of a dictionary

sample_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print(", ".join(sample_dict))

Output:

a, b, c, d

In the above example, we see that when joining a dictionary, its keys are joined together and not the values. If a dictionary has a non-string key, a TypeError exception will be raised if you try to join it using the string join() function.

For more on the join function refer to the python docs.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top