开发者

printing lines that contain 60 characters

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜