First of all, to see how a character can be processed each time, there are several ways to do it:
Method One:
Copy Code code as follows:
>>> a= ' 1234567 '
>>> List (a)
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ']
>>>
Method Two:
Copy Code code as follows:
>>> a= ' 1234567 '
>>> for I in a:
... print I
...
1
2
3
4
5
6
7
>>>
Method Three: List resolution (map)
Copy Code code as follows:
>>> A
' 1234567 '
>>> [Int (i) +1 for I in a]
[2, 3, 4, 5, 6, 7, 8]
>>>
But if you handle two characters or more characters at a time, the above method is not working, I summarize the following two kinds:
Method One: Use a fragment operation, processing two characters at a time:
Copy Code code as follows:
>>> 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
>>>
Method Two: Use regular expression, split string, processing 3 characters at a time
Copy Code code as follows:
>>> Import re
>>> a= "1234567890"
>>> for I in Re.findall (". { 1,3} ", a):
... print I
...
123
456
789
0
>>>
You can change the n characters at a time, depending on your requirements.