Python Join() Function

Python Join() Function

3 mins read354 Views Comment
clickHere
Updated on Jun 27, 2022 10:29 IST

During Python programming, you might encounter instances where you would need to create strings from iterable objects. For example, building a sentence from a sequence of words. For this, the predefined join() function in Python comes in handy. 

2022_06_join-python.jpg

In this article, we will guide you on how the python join() function works So, without further ado, let’s get started!

We will be covering the following sections today:

Python Join() Function Syntax

Here’s the syntax for the built-in string join function in Python:

string.join(iterable)

The function takes in an iterable as an argument and concatenates each element of an iterable (such as tuple, list, string, dictionary, and set) by using a string separator (the string on which the join() method is called).

The function returns a concatenated string. However, keep in mind that if the iterable contains any non-string values, the function would raise a TypeError.

Read: Introduction to Python – Features, Use Cases, Resources, and Applications

How Python Join() Method Works

With a tuple of strings

Here, we use the join() function to concatenate multiple strings in a tuple without any separator:

#Concatenate a tuple of strings without a separator
mytuple = ('Big', 'Mac', 'Burger')
string = ''.join(mytuple)
print(string)

Output:

output1

Now, what if we use a dash as a string separator? Look at the below example:

#Concatenate a tuple of strings with a - separator
mytuple = ('Big', 'Mac', 'Burger')
string = '-'.join(mytuple)
print(string)

Output:

output2

Let’s see what happens if there is a non-string element in the tuple:

mytuple = ('Ben', 10)
string = ' '.join(mytuple)
print(string)

Output:

output3

As expected, we get a TypeError exception because the tuple has an integer object which needs to be converted into a string type to allow concatenation:

mytuple = ('Ben', 10)
string = ' '.join(str(i) for i in mytuple)
print(string)

Output:

output4

What have we done here?

  • First, we created a tuple with different data type elements – a string and an integer object in this case.
  • Now, since the join() method only works for objects with the string data type, we used a generator expression to convert each element of the tuple to a string.
  • Then, we used join() to concatenate the string-type elements.

With a list of strings

Similarly, we can use the join() function to concatenate multiple strings in a list:

#Concatenate a list of strings with @ separator
colorlist = ['Red', 'Green', 'Blue']
string = '@'.join(colorlist)
print(string)

Output:

output5

With strings

We can use the join() function to concatenate string objects with the specified separator:

#Concatenate a string with a comma separator
string = 'AEIOU'
print(','.join(string))

Output

output6

We can also use another string as a separator. Let’s see how:

str1 = 'abc'
str2 = '123'
 
print(str1.join(str2))
 

Output:

output7

Here, we are using str1 as the separator for str2

With a dictionary of strings

Let’s see how we can use the join() function to concatenate the keys in a dictionary by  a string separator:

#Concatenate a dictionary of strings with + separator
 
#Create a dictionary
form = {'Name': 'Bob', 'Age': 25, 'Mobile': 4011352}   #Key is a string
 
#Join method on keys of the dictionary
print('+'.join(form))
 

Output:

output8

With a set of strings

Now, let’s see how we can use the join() function to concatenate multiple strings in a set by a string separator:

#Concatenate a set of strings with -> separator
set1 = {'Jaipur', 'Mumbai', 'Goa',}
separator = ' -> '
print('Flight Route:', separator.join(set1))

Output:

Endnotes

n and how to use it to concatenate multiple strings in an iterable into one string. Want to learn more about Python and practice Python programming? Explore related articles here.

Top Trending Tech Articles:
Career Opportunities after BTech | Online Python Compiler | What is Coding | Queue Data Structure | Top Programming Language | Trending DevOps Tools | Highest Paid IT Jobs | Most In Demand IT Skills | Networking Interview Questions | Features of Java | Basic Linux Commands | Amazon Interview Questions
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments