Table of contents
1.
Introduction
2.
What is Join() in Python?
2.1.
Python
3.
Syntax of Python join() Function
4.
Python join() Parameters
5.
Return Value of join() in Python
6.
String join() in Python Examples
6.1.
The join() method with sets
6.2.
Python
6.3.
The join() method with dictionaries
6.4.
Python
7.
Frequently Asked Questions 
7.1.
What is Join() in Python?
7.2.
What does join() do in code?
8.
Conclusion
Last Updated: Nov 19, 2024
Easy

Python String join() Method

Author Geetika Dua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Strings are a fundamental part of programming, and Python provides powerful tools to work with them. Among these tools, the join() method stands out as an efficient way to combine multiple strings into one. Whether you're merging a list of words, constructing sentences, or formatting data, join() offers a clean and intuitive approach to string concatenation. In this blog, we'll explore how the join() method works.

join in python

What is Join() in Python?

The first question that may arise in your mind is, what exactly is a join in python?

Join in Python is an inbuilt string function. It is used to join sequence elements separated by a string separator. The job of the merge function is to join the elements of a sequence and makes it a string. 

Syntax: string_name.join()

  • Python

Python

sample = ('we', 'are’, 'coding', 'ninjas')

# Put any character to join
s = "."
s = s.join(sample)
print(s)
You can also try this code with Online Python Compiler
Run Code
Output

Syntax of Python join() Function

The syntax for the Python join() Function is as follows:

StringSeperator.join(iterable)

Python join() Parameters

Python join() parameter consists of a single parameter,

  • iterable: The iterable can be of any type, including Tuple, List, Set, String, and Dictionary.

Return Value of join() in Python

The return value of join() in Python is of type String. It returns a string after the concatenation of all elements of iterable.

String join() in Python Examples

In this section we will discuss some examples of String join() in Python.

The join() method with sets

  • Python

Python

set_1 = {'Maths', 'Physics', 'Chemistry'}
separator = '+'
print(separator.join(set_1))
You can also try this code with Online Python Compiler
Run Code

Output

output

Explanation

In the above example, set_1 contains the element of a set that are concatenated with “+” which acts as the string separator.

The join() method with dictionaries

  • Python

Python

Dict_1 = {'X': 1, "Y": 2}
separator = '->'
print(separator.join(Dict_1))
You can also try this code with Online Python Compiler
Run Code

Output

output

Explanation

In the above example, the join() method joins the keys with the separator and ignores the values of the dictionary.

Frequently Asked Questions 

What is Join() in Python?

Join in Python is an inbuilt string function. It is used to join sequence elements separated by a string separator. The job of the merge function is to join the elements of a sequence and make it a string. 

What does join() do in code?

The join() method concatenates elements of an iterable (like a list or tuple) into a single string, using a specified separator.

Conclusion

This blog covered the Join in python. It introduced the concept of join() function in Python and explained its syntax, Parameters, and Return Value. We hope you are now familiar with this topic of Join in Python. Eager to learn more? Refer to our other blogs-

Live masterclass