python split into list code example
Example 1: how to split a string by character in python
def split(word):
return [char for char in word]
# Driver code
word = 'geeks'
print(split(word))
#Output ['g', 'e', 'e', 'k', 's']
Example 2: 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 3: python split string on char
>>> "MATCHES__STRING".split("__")
['MATCHES', 'STRING']