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
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
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
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
s = 'KDnuggets is a fantastic resource'
print(s.split())
# Output
# ['KDnuggets', 'is', 'a', 'fantastic', 'resource']
# By default, split() splits on whitespace,
# but other character(s) sequences can be passed in as well.
s = 'these,words,are,separated,by,comma'
print('\',\' separated split -> {}'.format(s.split(',')))
s = 'abacbdebfgbhhgbabddba'
print('\'b\' separated split -> {}'.format(s.split('b')))
# ',' separated split -> ['these', 'words', 'are', 'separated', 'by', 'comma']
# 'b' separated split -> ['a', 'ac', 'de', 'fg', 'hhg', 'a', 'dd', 'a']
xxxxxxxxxx
cmd = input('Enter command:')
dictionary = cmd.split(' ')[1]
text = cmd.split(' ')[2:]
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.