What is for Python what 'explode' is for PHP?

Rajasekar picture Rajasekar · Oct 4, 2010 · Viewed 99.6k times · Source

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.

What is the equivalent in Python?

Answer

SilentGhost picture SilentGhost · Oct 4, 2010

Choose one you need:

>>> s = "Rajasekar SP  def"
>>> s.split(' ')
['Rajasekar', 'SP', '', 'def']
>>> s.split()
['Rajasekar', 'SP', 'def']
>>> s.partition(' ')
('Rajasekar', ' ', 'SP  def')

str.split and str.partition