开发者

printing lines that contain 60 characters

开发者 https://www.devze.com 2023-01-05 08:22 出处:网络
I\'m having trouble printing a string in lines chat contains 60 characters. my code is below: s = \'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrtsucwxyz\'

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

精彩评论

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

关注公众号