Python String join() Method

Learn via video courses
Topics Covered

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.

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 Python join()

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 Python join()

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:

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:

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

Q. What is the purpose of the join() method in Python?

A: 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.

Q. How does the join() method work in Python?

A: 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.

Q. Can the join() method be used with other types of iterables besides lists?

A: 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.