Reputation: 3
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):
for k in s[i:i+60]:
print k
Upvotes: 0
Views: 1864
Reputation: 107666
You can also use the textwrap module, ie textwrap.fill(s, 60)
Upvotes: 2
Reputation: 32532
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]
Upvotes: 4
Reputation: 175615
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
Upvotes: 4