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

string.join(iterable)a

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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)

print(x)

Output:

JoininPython_1

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) 

print(s)

Output:

JoininPython_2.JoininPython_2.

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.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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)

Output:

JoininPython_3

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)

print(x)

Output:

JoininPython_4

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)

print(x)

Output:

JoininPython_5.

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

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)

print(x)

Output:

JoininPython_6

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)

print(x)

Output:

JoininPython_7.

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)

print(x)

y = s2.join(s1)

print(y)

Output:

JoininPython_8

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.

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 our PGP Full Stack Development 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!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 18 Dec, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449