Join in Python is an in-built method used to join an iterable’s elements, separated by a string separator, which is specified by you. Thus, whenever you want to join the elements of an iterable and make it a string, you can use the string join in Python.

Definition and Usage of Join in Python

The join in Python takes all the elements of an iterable and joins them into a single string. It will return the joined string. You have to specify a string separator that will be used to separate the concatenated string.

Syntax of Python join() Function


In the above syntax, the string is the separator string which you need to include between all the iterable elements.

Parameters Used in String Join in Python

  • iterable: As a parameter for string join in Python, you have to pass an iterable whose elements will be joined and returned

The iterable can be of any type, including:

Iterables can also be the file objects or the objects that are defined with an __iter__() or __getitem__() methods.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

Return Value of String Join in Python

The return value is a string created by joining the specified iterable’s elements.

Example: Working of the Join Function

In the code below, you will use the join() function to join the fruits stored in a set.

mySet = {"apple", "banana", "cherry"}

s = "-"

x = s.join(mySet)




As you can see in the output, the join() function joined all the three elements of the set with “-” as the separator.

Exceptions of the Join in Python

The Python join() function can throw a single exception, which is:

  • TypeError: The string() function will raise this error if the iterable contains any non-string value

Example: Getting the TypeError Exception

In the code below, you will create a list of integers and use the join() function. However, since you are using the integers, it will throw the TypeError exception.

lst = [1,2,3,4] 

s = ","

# using the join function

s = s.join(list1) 




As you can see in the above output, you get the TypeError. However, that does not mean you cannot join numbers. The workaround is to specify the numbers in single inverted comma characters (‘’). The inverted commas will convert the numbers to a string, and the join() function will join them. You will see the example for joining numbers in the next section when you explore using join in Python with a list.

Using Join in Python with Different Iterables

In this section, you will go through examples of using the string join in Python with various iterables.

Example: String Join in Python with List

In this example, you will create a list of vowels. You will then use the join in Python to return a string of vowels separated by a ‘,.’

vowel_lst = ['a', 'e', 'i', 'o', 'u']

s = ","

# using the join function

x = s.join(vowel_lst)

print("The Vowels are: ", x)



Example: Using Join in Python with Tuple

For this example, you will create a tuple with car names and use the Python join() function to get a string of car names separated by a ‘-.’ Also, here you will directly use the separator, instead of declaring it separately (something you haven’t seen yet).

tuple_example = ("BMW", "Ferrari", "Jeep", "Lamborghini", "Porsche")

# declaring the separator directly and using it

x = "-".join(tuple_example)




Example: Using Python join() Function with a Set

In the code below, you will write a greeting from Simplilearn as elements of a set and then use the join() function to print the returned string.

set_example = {"Welcome", "to", "Simplilearn"}

x = "-".join(set_example)




FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Example: String Join in Python with a Dictionary

Here, you will create a dictionary and use the join() method on it.

dict_example = {"Hello!": 1, "How": 2, "Are": 3, "You?": 4}

s = "->"

x = s.join(dict_example)




If you noticed, the dictionary values were integers, but the join() function didn’t throw the TypeError exception. That’s because the join in Python tries to join the dictionary’s keys and not the values. What if you reverse the dictionary to make integers the keys and strings as the values? Let’s have a look at what happens.

dict_example = {1: "Hello!", 2: "How", 3: "Are", 4: "You?"}

s = "->"

x = s.join(dict_example)




As expected, you got a TypeError exception when the join() function tried to join the keys; it got integer values.

Example: Using Join in Python with String

You can also use the join() function with a string. While using the join function with a string, you can also use one of the strings as a separator. Let’s look at the example depicted below, and then can further explore it.

s1 = "Hello"

s2 = "World"

x = s1.join(s2)


y = s2.join(s1)




As you can see, when you used s1 as the separator and joined the s2, the function separated each character of the s2 with the entire s1 string and vice versa when you used s2 as the separator and joined the s1.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

Summing It Up

In this article, you have learned everything about join in Python. You can use this built-in function to join any iterable you want. Using the Python join function() is a fundamental concept in Python. You can learn more about functions here

If you are a newbie and want to get acquainted with more such fundamental Python programming concepts, you can refer to Simplilearn’s Python Tutorial for Beginners. The beginner's tutorial is adept at helping you get your basics clear. Once you are done with that, you can opt for the Online Python Certification Course. With a vast range of learning materials and applied learning, the course can help you excel in the field of Python development.

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, at the earliest.

Happy learning!

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors