In this tutorial, we will discuss the Python join() method and learn how to use it. Join() can be used to combine strings, lists, and dictionaries. We will provide several examples to help you understand its usage.
The primary use case of this method is to join a sequence of types like strings, tuples, numbers, and dictionaries and convert them into a String. It is quite a simple method to use which concatenates elements of a collection with a separator. Let’s now take a sequence of each data type one by one and tries to join with examples.
Python Join() Description & Syntax
The join() method allows an iterable as the input parameter and concatenates its elements into a string with some separator. We can pass any string separator such as a comma, space, or an underscore, etc. Interestingly, you can even specify an actual string value such as “XYZ” or “ABC,” etc.
It has the following syntax which you need to remember for use in Python programs.
# Python Join Syntax string_sep.join( python_seq )
Here, string_sep
is the separator or delimiter that you want to use to join the elements, and python_seq
is the collection of elements that you want to join.
The join() method is particularly useful when you have a list of strings and you want to combine them into a single string.
Join Strings in a List
We can pass a list of string type values or chars to join() method. It will concatenate all the words in a list or each letter in the case of characters. The returned value is a unified string object connected with a given separator.
Languages = ['Python', 'CSharp', 'JavaScript', 'AngularJS'] token = ', ' print(token.join(Languages)) Chars = ['a', 'g', 'i', 'l', 'e', ' ', 's', 'c', 'r', 'u', 'm'] token = '' print(token.join(Chars))
The first sample in the above example will produce a comma-separated string. The second one would just concatenate all the characters into one string. See the results below.
Python, CSharp, JavaScript, AngularJS agile scrum
Join Numbers in a List
The input sequence could hold a group of integers. In this case, we first need to convert each of them to a string and then join them.
Primes = ['2', '3', '5', '7', '11', '13'] token = '->' print(token.join(str(iter) for iter in Primes)) mixedList = ['I', 'work', 24, 'hrs', 'a', 'day', 'and', 365, 'days', 'a', 'year.'] token = ' ' print(token.join(str(iter) for iter in mixedList))
Check the output below:
2->3->5->7->11->13 I work 24 hrs a day and 365 days a year.
Concatenate Elements of a Tuple
The tuple is also a type of sequence that we can pass to join(). It then combines all its elements into one.
inputTuple = ("Test1", "Test2", "Test3", "Test4") sep = '_' out = sep.join(inputTuple) print(out)
Output:
Test1_Test2_Test3_Test4
Let’s check out another tuple example. In this, the tuple has mixed type values. Hence, we’ll first need to convert each of them to a string using the map() function.
inputTuple = ("Test", 1, "Test", 2, "Test", 3, "Test", 4) sep = '_' out = sep.join(map(str, inputTuple)) print(out)
Output:
Test_1_Test_2_Test_3_Test_4
Combine Keys of a Dictionary
Python join() method can also accept a dictionary and combines all its key fields as one. See the below example.
inputDict = {"Counry": "USA", "City": "NewYork", "Street": "Wall Street"} separator = "_" out = separator.join(inputDict) print("After join: " + out) print("Join() return type: {}".format(type(out)))
The output:
After join: Counry_City_Street Join() return type: <class 'str'>
Join Elements in a Set
Let’s see now how Join() works when a set type sequence is passed as its argument.
GangOfFour = {'Gamma', 'Helm', 'Johnson', 'Vlissides'} token = '_' print(token.join(GangOfFour)) SetOfPrimes = {'2', '3', '5', '7', '11', '13'} token = ', ' print(token.join(SetOfPrimes))
The result is as follows:
Vlissides_Gamma_Helm_Johnson 13, 3, 2, 7, 11, 5
Must Read – Convert List to String