string = 'James Smith Bond'
x = string.split(' ')
print('The name is',x[-1],',',x[0],x[-1])
s = 'KDnuggets is a fantastic resource'
print(s.split())
s = 'these,words,are,separated,by,comma'
print('',' separated split -> {}'.format(s.split(',')))
s = 'abacbdebfgbhhgbabddba'
print(''b' separated split -> {}'.format(s.split('b')))
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.
split:
s="ab.1e.1e3"
w = s.split('.1')
// w is ["ab","e","e3"]
print("ap1oo11rv1".split("1"))
// prints ['ap', 'oo', '', 'rv', '']
// use help(str.split) in your python IDE to know split in detail.
// if .split() is called then split is done wrt successive whitespaces.
ex.1 print(" a pp p ".split())
//prints ['a', 'pp', 'p']
// .split(' ') is different
ex.2 print(" a pp p ".split())
//prints ['', '', '', '', '', 'a', '', '', 'pp', '', '', 'p', '']
join:
w=["as","3e","1"]
a='vf'.join(w)
// w neccessarily needs to be a list of strings.
print(a)
//displays string asvf3evf1
- both split and join undo the effect of each other
- we can write directly in 1 line as well:
print("QR".join("ap1oo1rv1".split("1")))
// prints apQRooQRrvQR