python string split by separator

How to split string in python with ot without regex, single or multiple characters:

Some tricky examples showing how to deal with string split in python when you have separator or even more than one.

Python string split

String split in python with separators comma, dot, whitespace:

numbers = "python is cool language"
print(numbers.split()) #split string by whitespace
numbers = "python,is,cool,language"
print(numbers.split(",")) #split string by comma into a list
numbers = "python.is.cool.language"
print(numbers.split(".")) #split string by dot

result:

['python', 'is', 'cool', 'language']
['python', 'is', 'cool', 'language']
['python', 'is', 'cool', 'language']

Python string split by several separators with regex

By using a regex you can split strings into lists very easy. Below you can see two examples:

  • r"[\w']+" - find all words and extract them into a list
  • r"[\d']+" - split a number string into list by regex
text = "python is cool language!? while..java is not that cool"
print (re.findall(r"[\w']+", text))

number = "1000,000,000.000.000"
print (re.findall(r"[\d']+", number))

result:

['python', 'is', 'cool', 'language', 'while', 'java', 'is', 'not', 'that', 'cool']
['1000', '000', '000', '000', '000']

Python string split by another string

In python you can split string by another string by using the same split method:

numbers = "python is cool language"
print(numbers.split("cool")) #split string by string

result:

['python is ', ' language']

Python string split several separators no regex

If you don't want to use regex(regular expression) or you have small known set of separators then you can go for alternative solution to replace all separators by other one like space and then to do a split:

text = "python is cool language!? while..java is not that cool"
seps = ["?", ".", "!"]
for sep in seps:
    text = text.replace(sep,' ')
print(text.split())

result:

['python', 'is', 'cool', 'language', 'while', 'java', 'is', 'not', 'that', 'cool']

Related Article