小编unr*_*l4u的帖子

寻找PHP的str_split()替换

假设我有一小段代码:

<?php 
$tmp = str_split('hello world!',2);
// $tmp will now be: array('he','ll','o ','wo','rl','d!');
foreach($tmp AS &$a) {
  // some processing
}
unset($tmp);
?>
Run Code Online (Sandbox Code Playgroud)

我怎么能在Python v2.7中做到这一点?

我以为这样做会:

the_string = 'hello world!'
tmp = the_string.split('',2)
for a in tmp:
  # some processing
del tmp
Run Code Online (Sandbox Code Playgroud)

但它返回一个"空分隔符"错误.

有什么想法吗?

python string

1
推荐指数
1
解决办法
829
查看次数

标签 统计

python ×1

string ×1