Craig
Craig

Reputation: 3

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):
    for k in s[i:i+60]:
        print k

Upvotes: 0

Views: 1864

Answers (3)

Jochen Ritzel
Jochen Ritzel

Reputation: 107666

You can also use the textwrap module, ie textwrap.fill(s, 60)

Upvotes: 2

Sam Dolan
Sam Dolan

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

Michael Mrozek
Michael Mrozek

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

Related Questions