split string into list python code example
Example 1: split string by length python
>>> x = "qwertyui"
>>> chunks, chunk_size = len(x), len(x)/4
>>> [ x[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]
['qw', 'er', 'ty', 'ui']
Example 2: 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 3: 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 4: split string python
file='/home/folder/subfolder/my_file.txt'
file_name=file.split('/')[-1].split('.')[0]
Example 5: python split string
UserInputs = []
UserInputs = input().split()
Example 6: how to split a string in python
sample_str = "Help developers by donating!"
txt.split(' ')