I'm having trouble printing a string in lines chat contains 60 characters.
my code is below:
s = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrtsucwxyz'
for i in range(0, len(s), 60):
开发者_如何学Gofor k in s[i:i+60]:
print k
s[i:i+60]
will slice the 60 characters you want into a string. By adding a second for loop, you're looping over each character in that string and outputting it separately. Just output s[i:i+60]
instead
Print the slice itself, not each character in the slice.
s = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrtsucwxyz'
for i in range(0, len(s), 60):
print s[i:i+60]
You can also use the textwrap module, ie textwrap.fill(s, 60)
精彩评论