How to Use the join() String Method in Python
In this tutorial, you'll learn how to use the join() string method in Python. The join() string method returns a string, which is the concatenation of the elements in an iterable. It takes all items in the iterable and concatenates them, and returns the concatenated string.
Python join() String Method Syntax
The syntax of the join() string method is as follows:
string.join(iterable)
The join() Method Parameter
- iterable: Required. It can be a list, tuple, dictionary, or set.
Remark
- Python raises a TypeError if there are any non-string values in iterable.
The join() String Method Examples
Example 1: Join Items in a List
The following code is an example of how to concatenate all the strings in a list, which is separated by a vertical bar and returns the concatenated string:
vegetables = ["Garlic", "Tomato", "Cabbage", "Lettuce", "Celery", "Cucumber", "Carrots", "Broccoli"]
concatenated_string = "#".join(vegetables)
print (concatenated_string)
Output:
Garlic|Tomato|Cabbage|Lettuce|Celery|Cucumber|Carrots|Broccoli
Example 2: Join Items in a Tuple
The following code illustrates an example of how to concatenate all the strings in a tuple, which is separated by a vertical bar and returns the concatenated string:
vegetables = ("Onion", "Tomato", "Cabbage", "Okra", "Celery", "Cucumber", "Carrots", "Chilli peppers")
concatenated_string = "|".join(vegetables)
print (concatenated_string)
Output:
Onion|Tomato|Cabbage|Okra|Celery|Cucumber|Carrots|Chilli peppers
Example 3: Join Keys of a Dictionary
The following code is an example of how to concatenates all the keys in a dictionary, separated by a vertical bar, and returns the concatenated string:
student_info = {
"ID": "#10001",
"Name": "James",
"DateOfBirth": "July 20th 2000",
"Mobile_Phone": "0012-0200-02002"
}
concatenated_string = "|".join(student_info)
print (concatenated_string)
Output:
ID|Name|DateOfBirth|Mobile_Phone
Note that when you use the join() string method on a dictionary, join() takes all the keys, concatenates them, and returns the concatenated string.
Example 4: Join Items in a Set
Join all the items in a set, which is separated by a single space, and returns the concatenated string:
email_addresses = {"john@domain1.com", "james@domain2.com", "roland@domain3.com", "karl@domain4.com"}
concatenated_string = " ".join(email_addresses)
print (concatenated_string)
Output:
john@domain1.com karl@domain4.com james@domain2.com roland@domain3.com
Example 5: TypeError
Join all the items in a list, which is separated by a single space, and returns the concatenated string:
fruits = ["Apple", 2, "Melon"]
concatenated_string = " ".join(list)
print (concatenated_string)
Output:
Traceback (most recent call last):
File "string_join.py", line 26, in
concatenated_string = " ".join(list)
TypeError: can only join an iterable
As you can see, Python threw TypError since the second item in the list was non-string value.
In this tutorial, you've learned how to use the Python join() string method. The join() string method takes all elements in an iterable, such as a list, tuple, dictionary, or set, and then concatenates them into a single string, separated by a specified character.