Join() function

The join() function can be used to either join lists or strings with a common string.

The basic format for using this function:

'character'.join(string/list)

(1) Joining lists:

We can all the list elements with a common character. Let's say that we have a list with the first 5 English alphabets and let's say that we want to join these elements with a underscore.

>>> alphabets = ['a','b','c','d','e']
>>> '_'.join(alphabets)
'a_b_c_d_e'

(2) Joining strings:

We can use this same formatting to join the strings.

>>> string = 'abc'
>>> '-'.join(string)
'a-b-c'