python split string to list code example
Example 1: split string python
string = 'James Smith Bond'
x = string.split(' ')
print('The name is',x[-1],',',x[0],x[-1])
Example 2: python split by list
def split(txt, seps):
default_sep = seps[0]
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 list
string = 'this is a python string'
wordList = string.split(' ')
Example 4: python string to list without split
sentence = 'This is a sentence'
split_value = []
tmp = ''
for c in sentence:
if c == ' ':
split_value.append(tmp)
tmp = ''
else:
tmp += c
if tmp:
split_value.append(tmp)
Example 5: python split list
string = "this is a string"
splited_string = string.split(" ")
print(splited_string)