Splitting strings in Python, mostly using the str.split method.

Split by whitespace

Note that an empty string will give an error but you can use no arguments for the default.

That handles tabs, newlines and spaces.

my_string.split()

e.g.

"abc def ghi klm".split()
# ['abc', 'def', 'ghi', 'klm']
"abcdef".split()
# ['abcdef']
"abc\tdef\nghi klm".split()
# ['abc', 'def', 'ghi', 'klm']

Split by delimeter

Simple

my_string.split(":")
"abc:def:ghi".split(":")
# ['abc', 'def', 'ghi']

Remove whitespace

Split by a sequence of a command and a space.

"abc, def, ghi, klm".split(", ")
# ['abc', 'def', 'ghi', 'klm']

If items may or may not have space as in the next example, you can use str.strip on each items to remove whitespace if any.

[x.strip() for x in "abc, def,ghi, klm".split(",")]
# ['abc', 'def', 'ghi', 'klm']

Warning - here the whitespace is retained in each item, because we only split by a comma.

"abc, def, ghi, klm".split(",")
# ['abc', ' def', ' ghi', ' klm']

Split by newline characters

my_string.split("\n")

e.g.

"""abc
def
ghi
""".split("\n")
# ['abc', 'def', 'ghi', '']

If reading text from a file, you’ll end up with a trailing newline in each string still. Then this more appropriate:

my_string.splitlines()

e.g.

with open(path) as f_in:
    text = f_in.read()
    lines = text.splitlines()

Or not from a file.

"""abc
def
ghi
""".splitlines("\n")
['abc', 'def', 'ghi']

This will split by the newline characters and will also remove them by default. This is useful as you probably don’t want to have "\n" at the end of each item in the list.

String to list of characters

You cannot use the str.split method here. No argument or a null strings returns the original string while an empty string gives an error.

Use list.

list("abcdef ghi")
# ['a', 'b', 'c', 'd', 'e', 'f', ' ', 'g', 'h', 'i']