The join
method in Python takes elements of an iterable data structure and connects them together using a particular string connector value.
join
work?The join
method in Python is a string method which connects elements of a string iterable structure, which also contains strings or characters (array, list, etc.) by using a particular string as the connector.
The illustration below shows how this method works.
Now that we know how the function works, let’s look at examples to further enhance our understanding.
This will join the elements in array
using an empty string between each element.
array = ['H','E','L','L','O']connector = ""joined_string = connector.join(array)print(joined_string)
,
This will join the elements in array
using a comma between each element.
array = ['1','2','3','4','5']connector = ","joined_string = connector.join(array)print(joined_string)
This shows how elements can be connected by a dash, without having to create a separate string variable for the connector.
array = ['W','O','R','L','D']print("-".join(array))