I had a string which is stored in a variable myvar="Rajasekar SP"
. I want to split it with delimiter like we do using explode in PHP. But dont know the alternative for the explode in python. Please help
views:
117answers:
2
+7
A:
Choose one you need:
>>> s = "Rajasekar SP def"
>>> s.split(' ')
['Rajasekar', 'SP', '', 'def']
>>> ['Rajasekar', 'SP', 'def']
['Rajasekar', 'SP', 'def']
>>> s.partition(' ')
('Rajasekar', ' ', 'SP def')
SilentGhost
2010-10-04 11:40:30
+2
A:
The alternative for explode in php is split.
The first parameter is the delimiter, the second parameter the maximum number splits. The parts are returned without the delimiter present (except possibly the last part). When the delimiter is None, all whitespace is matched. This is the default.
>>> "Rajasekar SP".split()
['Rajasekar', 'SP']
>>> "Rajasekar SP".split('a',2)
['R','j','sekar SP']
Peter Smit
2010-10-04 11:48:31