时间:2021-07-01 10:21:17 帮助过:64人阅读
代码如下:
>>> a='1234567'
>>> list(a)
['1', '2', '3', '4', '5', '6', '7']
>>>
代码如下:
>>> a='1234567'
>>> for i in a:
... print i
...
1
2
3
4
5
6
7
>>>
代码如下:
>>> a
'1234567'
>>> [int(i)+1 for i in a]
[2, 3, 4, 5, 6, 7, 8]
>>>
方法一:使用分片操作,每次处理两个字符:
代码如下:
>>> a='abcdefghijk'
>>> num=0
>>> while True:
... str = a[num:num+2]
... if str:
... print str
... else:
... break
... num += 2
...
ab
cd
ef
gh
ij
k
>>>
代码如下:
>>> import re
>>> a="1234567890"
>>> for i in re.findall(".{1,3}",a):
... print i
...
123
456
789
0
>>>