TutorialAce

Python | Java | MySQL | Excel

Python Tutorial

Python join() String Method with Examples

How to Use the String join() Method in Python

In this tutorial, we'll learn how to use the string join() method in Python.

The string join() 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 String join() Syntax

The syntax of join() is as follows:

string.join(iterable)

The join() Method Parameter

  • iterable: Required. It can be a list, tuple, dictionary or set.

Remark

  • A TypeError will be raised if there are any non-string values in iterable.

String join() Method Examples

Example 1: join() with a List

The following code concatenates all items in a list, using a vertical bar as separator , and returns the concatenated string:

vegetables = ["Garlic", "Tomato", "Cabbage", "Lettuce", "Celery", "Cucumber", "Carrots", "Broccoli"]
concatenated_string = "#".join(vegetables)
print (concatenated_string)

Result:

Garlic|Tomato|Cabbage|Lettuce|Celery|Cucumber|Carrots|Broccoli

Example 2: join() with a Tuple

The following code concatenates all items in a tuple, using a vertical bar as separator, and returns the concatenated string:

vegetables = ("Onion", "Tomato", "Cabbage", "Okra", "Celery", "Cucumber", "Carrots", "Chilli peppers")
concatenated_string = "|".join(vegetables)
print (concatenated_string)

Result:

Onion|Tomato|Cabbage|Okra|Celery|Cucumber|Carrots|Chilli peppers

Example 3: join() with a Dictionary

The following code concatenates all key in a dictionary, using a vertical bar as separator, 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)

Result:

ID|Name|DateOfBirth|Mobile_Phone

Note that when using the string join() method on a dictionary, join() takes all keys and concatenates them into a string.

Example 4: join() with a Set

The following code concatenates all items in a set, using a single space as separator, 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)

Result:

john@domain1.com karl@domain4.com james@domain2.com roland@domain3.com

Example 5

The following code concatenates all items in a list, using a single space as separator , and returns the concatenated string:

fruits = ["Apple", 2, "Melon"]
concatenated_string = " ".join(list)
print (concatenated_string)

Result:

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, we've learned how to use the Python string join() method. The string join() method takes all elements in an iterable (such as a list, tuple, dictionary or set) and concatenates them into a single string.

Leave a Reply