xxxxxxxxxx
text= 'Love thy neighbor'
# splits at space
print(text.split())
grocery = 'Milk, Chicken, Bread'
# splits at ','
print(grocery.split(', '))
# Splits at ':'
print(grocery.split(':'))
"""
Output
['Love', 'thy', 'neighbor']
['Milk', 'Chicken', 'Bread']
['Milk, Chicken, Bread']
"""
xxxxxxxxxx
spam = "A B C D"
eggs = "E-F-G-H"
# the split() function will return a list
spam_list = spam.split()
# if you give no arguments, it will separate by whitespaces by default
# ["A", "B", "C", "D"]
eggs_list = eggs.split("-", 3)
# you can specify the maximum amount of elements the split() function will output
# ["E", "F", "G"]
xxxxxxxxxx
string = 'James Smith Bond'
x = string.split(' ') #Splits every ' ' (space) in the string to a list
# x = ['James','Smith','Bond']
print('The name is',x[-1],',',x[0],x[-1])
xxxxxxxxxx
file='/home/folder/subfolder/my_file.txt'
file_name=file.split('/')[-1].split('.')[0]
xxxxxxxxxx
>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']
xxxxxxxxxx
path = "xlsx_files/risk_on/feature/US10Y.csv"
name = path.split("/")
print(name)
#['xlsx_files', 'risk_on', 'feature', 'US10Y.csv']
xxxxxxxxxx
a='Beautiful_abs, asd is ; better*than\nugly.dat'
import re
re.split('; |\.|,|_|\t+| +|\*|\n',a)
# you can add seperated term inside | | eg if you want to selerte by $ |$|
output ['Beautiful', 'abs', '', 'asd', 'is', '', 'better', 'than', 'ugly','dat']
# use as below for white spaces only
a.split()
output : ['Beautiful_abs,', 'asd', 'is', ';', 'better*than', 'ugly.dat']
xxxxxxxxxx
s="ab.1e.1e3"
w = s.split('.1')
// w is ["ab","e","e3"]
// use help(str.split) in your python IDE to know split in detail.