Python String join()
Python String join() method
Python String join() method joins the strings in the given iterable by the separator string and returns the resulting string.
We call join() method on the separator string, and pass the strings to be joined as argument.
Syntax of join() method
The syntax of String join() method is
str.join(iterator)
where str
is the separator string.
join() method takes one parameter.
Parameter | Description |
---|---|
iterator | Required An iterator containing strings to be joined. It can be a list of strings, a tuple of string, a set of string, or any iterator. |
For example, if str_1
, str_2
, and so on are the string items in the iterator, then the resulting string would be as shown in the following expression.
str_1 + str + str_2 + str + str_3 + ... + str_N
Return value
join() method returns a string value.
Exceptions
join() method raises TypeError exception if any of the items in the iterable is not a string, or if an iterable is not given as argument.
Examples
1. Join two strings with a space in Python
In this example, we will take two strings in str_1
and str_2
, and join these strings with a single space.
Python Program
str_1 = 'Hello'
str_2 = 'World'
result = ' '.join([str_1, str_2])
print(result)
Output
Hello World
2. Join strings in list with specific separator string in Python
In this example, we take a list my_list containing four strings, and join these strings with '--'
string as separator.
Python Program
my_list = ['apple', 'banana', 'cherry']
result = '--'.join(my_list)
print(result)
Output
apple--banana--cherry
3. Join strings in tuple in Python
In this example, we take a tuple with three string values, and join these strings with comma as separator.
Call join() method on comma string ','
, and pass the set object as argument.
Python Program
my_tuple = ('apple', 'banan', 'cherry')
result = ','.join(my_tuple)
print(result)
Output
apple,banan,cherry
4. Join strings in set in Python
In this example, we take some strings in a set, and join these strings with comma as separator.
Call join() method on comma string ','
, and pass the set object as argument.
Python Program
my_set = {'apple', 'banana', 'cherry', 'fig'}
result = ','.join(my_set)
print(result)
Output
fig,apple,cherry,banana
Since the items in a set are not ordered, we cannot guarantee the order of the strings. When you run the above program, the order of the strings in the resulting string may vary.
5. Stain join() method TypeError: sequence item 1: expected str instance, int found
In this example, we shall what happens if one of the items in the iterator (passed as argument to join() method) is not a string.
Let us take a list, where the second element is not a string, but an integer.
Call join() method on comma string ','
, and pass the set object as argument.
Python Program
my_list = ['apple', 25, 'cherry']
result = ','.join(my_list)
print(result)
Output
Traceback (most recent call last):
File "/Users/pythonexamplesorg/main.py", line 2, in <module>
result = ','.join(my_list)
^^^^^^^^^^^^^^^^^
TypeError: sequence item 1: expected str instance, int found
Therefore, we have to make sure that the items in the given argument iterator are of only string type.
Summary
In this tutorial of Python String Methods, we learned about String lower() method, its syntax, and how to use this method to join strings with various examples.