The join method for strings is confusing for many beginners, so here are some examples. The first one, that applies the join method to an empty string, is the simplest and yet perhaps the most confusing for some beginners.
In [1]:
''.join(['hello', 'gnew', 'world'])
Out[1]:
In [2]:
' '.join(['hello', 'gnew', 'world'])
Out[2]:
In [3]:
','.join(['hello', 'gnew', 'world'])
Out[3]:
In [4]:
', '.join(['hello', 'gnew', 'world'])
Out[4]:
In [5]:
' and '.join(['hello', 'gnew', 'world'])
Out[5]:
It "does nothing" gracefully.
In [6]:
' and '.join(['hello', 'gnew'])
Out[6]:
In [7]:
' and '.join(['hello'])
Out[7]:
In [8]:
' and '.join([])
Out[8]:
It accepts any kind of iterable that gives strings.
The following example uses a trivial generator function.
In [9]:
def foo():
yield 'hello'
yield 'gnew'
yield 'world'
In [10]:
' and '.join(foo())
Out[10]: