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

后端 未结 2 1307
天命终不由人
天命终不由人 2020-12-13 17:01

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 eq

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 17:08

    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']
    

提交回复
热议问题