python计算两字符串中的位置,从Python中的字符串中获取两个字符
how to get in python from string not one character, but two?
I have:
long_str = 'abcd'
for c in long_str:
print c
and it gives me like
a
b
c
d
but i need to get
ab
cd
I'm new in python.. is there any way?
解决方案for i, j in zip(long_str[::2], long_str[1::2]):
print (i+j)
or
import operator
for s in map(operator.add, long_str[::2], long_str[1::2]):
print (s)
itertools also provide a generalized implementation of this:
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)