python split string into list code example

Example 1: python split by list

def split(txt, seps):
    default_sep = seps[0]
    # we skip seps[0] because that's the default separator
    for sep in seps[1:]:
        txt = txt.replace(sep, default_sep)
    return [i.strip() for i in txt.split(default_sep)]


>>> split('ABC ; DEF123,GHI_JKL ; MN OP', (',', ';'))
['ABC', 'DEF123', 'GHI_JKL', 'MN OP']

Example 2: python split list

string = 'this is a python string'
wordList = string.split(' ')

Example 3: split string to list

>>> list("Word to Split")
['W', 'o', 'r', 'd', ' ', 't', 'o', ' ', 'S', 'p', 'l', 'i', 't']

Example 4: how to split word in python

text= "I am Batman"
splitted_text= text.split()

Print(splitted_text)

Tags:

Misc Example