Python String join() method
Python String join() method
The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string.
Syntax
string.join(seq)
Parameter
Seq: This parameter represents any iterable object where all the returned values are strings.
Return
This method returns the concatenated string.
Example 1
# Python program explaining
# the string.join() method
# initializing the list with integer values
List = ['11', '12', '13', '14']
print("List:",List)
comma = ', '
# concatenating the comma with the list
print("After join() method...")
print("List:",comma.join(List))
string1 = 'Hello'
string2 = '!!!'
# concatenating each character of string2 to the front of string1
print('\nstring1.join(string2):', string1.join(string2))
# concaneting each character of string1 to the front of string1
print('string2.join(string1):', string2.join(string1))
Output
List: ['11', '12', '13', '14'] After join() method... List: 11, 12, 13, 14 string1.join(string2): !Hello!Hello! string2.join(string1): H!!!e!!!l!!!l!!!o
Example 2
# Python program explaining
# the string.join() method
# initializing the list with interger values
List = {'2', '1', '3'}
seperator = ', '
print(seperator.join(List))
# initializing set
Set = {'Python', 'Java', 'Ruby'}
seperator = '->->'
# concanating each value of set at the front of the seperator
print(seperator.join(Set))
Output
1, 3, 2 Ruby->->Python->->Java
