Join in Python
Learn via video courses
Overview
The join() function in Python is an inbuilt function used to concatenate string elements of an iterable. The final concatenation remains separated by a string separator specified by the program.
What is join() Function in Python?
One of the most important operations that we can perform with elements is concatenation. Concatenation is the action of linking characters together in sequence in such a way that the string resulting from concatenating two different contains both of the strings in that particular order.
Aside from strings, we might also have iterables (an iterable refers to anything that can be 'looped over') whose elements we'd like to join for a particular reason.
There are many ways to combine two or more different strings in Python, and one of them is the join() function, which concatenates the strings with a separator in between them. A string separator is a character or group of characters that separate two concatenated strings. They are also part of the concatenated output.
For example, let the first string be 'abc' and the second string be '123'. If 'X' is taken as the string separator, the final concatenated string would be 'abc' + 'X' + '123', resulting in 'abcX123'. Notice how the string separator 'X' sits between the first and second strings in the output.
Example
An example to show the working of join is as follows:
Output:
Explanation:
- The elements is iterable of type list.
- In str1, the elements are joined or concatenated with no string separators.
- In str2, however, the elements are joined/concatenated with the - character acting as the separator.
:::
Syntax of join() Function in Python
The syntax of join() is as follows:
Parameter of join() Function in Python
The join method takes a single parameter,
- iterable: The iterable can be of any type, including List, Tuple, Dictionary, Set, and String.
Return Value of join() Function in Python
Return Type: string
It returns a string after concatenating all elements of iterable.
Examples to Understand Join In Python
Example 1: Working of join() Method With Strings
Output:
Explanation:
In this example, we've taken two strings a and b and applied join() on them along with the string seperator '-'. This created a string where a and b are concatenated together with each element of both strings seperated by the string seperator '-'.
Example 2: Working of join() Method With Tuples And Sets
Code 1: Working with Tuples
Output:
Code 2: Working with Sets
Output:
Explanation:
In the above examples, tuple1 contains the elements of tuple and join method concatenated the elements to a string with '-' acting as the string seperator. The set1 contains the elements of set which are concatenated with '+' acting as the string seperator.
Example 3: Working of join() Method With An Empty String
Output:
Explanation:
In this example, we have no string separators. This results in Python concatenating the elements of an iterable with no separation. As we can see above, the individual elements are concatenated to form the word 'HELLO'
Example 4: Working of The join() Method With Dictionaries
Output:
Explanation:
From this example, we can notice that the join() method joins the keys with the separator, and ignores the values of the dictionary.
Now another point to note is that the key of the string should be a string. If it isn’t, the join() method will raise a TypeError.
Output:
Example 5: Working of The join() Method With Dictionaries
Output:
Explanation:
From this example, we can notice that the join() method joins the keys with the separator, and ignores the values of the dictionary.
Now another point to note is that the key of the string should be a string. If it isn’t, the join() method will raise a TypeError.
Output:
Exception
If the iterable contains any non-string values, then a TypeError is raised. TypeError's are raised whenever an operation is performed on an incorrect or unsupported object type.
Output:
Example 6: Working of The join() Method With Lists
Output:
Hello World Python
Explanation:
In this example, the join() method combines the elements of the list my_list into a single string, with each element separated by a space. The separator is specified using the separator variable passed to the join() method.
FAQs
Q1. What is the purpose of the join() method in Python?
Ans: The join() method is used to concatenate a list of strings into a single string, with a specified separator between each element. It allows you to join the elements of a list efficiently without using loops or manual string concatenation.
Q2. How does the join() method work in Python?
Ans: The join() method is called on a string that serves as the separator. It takes an iterable (such as a list) containing strings as its argument. The join() method concatenates all the strings in the iterable, placing the separator string between them, and returns a single joined string.
Q3. Can the join() method be used with other types of iterables besides lists?
Ans: Yes, the join() method can be used with any iterable that contains strings. This includes tuples, sets, and even generator expressions. As long as the iterable contains strings, you can use the join() method to concatenate them with a specified separator.
:::
Conclusion
In this article, we’ve seen how the join() method in function works. We can use this in-built method to concatenate any iterable according to requirements.
-
The join() concatenates the iterables with the separators in-between.
- The elements of iterable should be of string type.
-
In dictionaries, join() concatenates the keys, not the values. If the keys in the dictionaries are not strings, a TypeError is raised.