The join()
method is a string method in Python that concatenates the elements of an iterable using the string as a delimiter between each element. It returns a new string containing all the elements of the iterable joined together with the specified string.
Parameter Values
Parameter | Description |
---|---|
iterable | An iterable (list, tuple, string, etc.) whose elements will be joined with the string calling the method. |
Return Values
The join()
method returns a string
formed by concatenating iterable elements.
How to Use join()
in Python
Example 1:
The join()
method returns a string concatenated with the elements of an iterable. It joins each element by a specified separator.
colors = ['red', 'green', 'blue']
result = ', '.join(colors)
print(result) # Output: red, green, blue
Example 2:
The join()
method can be used with a string as the separator to concatenate characters of a string.
word = 'hello'
result = '-'.join(word)
print(result) # Output: h-e-l-l-o
Example 3:
The join()
method is commonly used to create a CSV string from values in a list.
values = [1, 2, 3, 4, 5]
result = ','.join(map(str, values))
print(result) # Output: 1,2,3,4,5