Intermediate Python

Split a string

You can split any string using the str.split function. By default it splits on spaces:

In [1]:
s = "what is your name"
s.split()
Out[1]:
['what', 'is', 'your', 'name']

Join a list

You can join a list together by using the str.join function. Note that in front of the dot you put the string that you want to join with, and you pass the list you want to join together as an argument.

In [2]:
l = ["a", "b", "c"]
"-".join(l)
Out[2]:
'a-b-c'
In [3]:
l = ["a", "b", "c"]
":".join(l)
Out[3]:
'a:b:c'
In [4]:
l = ["a", "b", "c"]
" ".join(l)
Out[4]:
'a b c'