对于Python来说,什么是“爆炸式”?对于PHP来说,什么是“爆炸式”?

时间:2022-07-31 15:44:27

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.

我有一个字符串,它存储在变量myvar = "Rajasekar SP"中。我想把它和分隔符分开,就像我们在PHP中使用爆炸一样。

What is the equivalent in Python?

在Python中等价的是什么?

2 个解决方案

#1


118  

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

str.split和str.partition

#2


14  

The alternative for explode in php is split.

在php中爆发的替代方案是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']

#1


118  

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

str.split和str.partition

#2


14  

The alternative for explode in php is split.

在php中爆发的替代方案是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']