0

Possible Duplicate:
Iterate an iterator by chunks (of n) in Python?

Say I have a string of 492 characters stored in a single variable. How might I print out the first 50 characters, then go to the next line, then print out the next 50, finally printing a line with 42 characters?

Community
  • 1
  • 1
Zoë Clark
  • 1,334
  • 2
  • 13
  • 25

4 Answers4

6
chars_per_line = 50
for i in range(0, len(s), chars_per_line):
    print s[i:i+chars_per_line]
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
3
for line in mystring.splitlines():
    print line[:50]
Alex L
  • 8,748
  • 5
  • 49
  • 75
  • It looks like the OP has one long string that may not necessary contain newlines. – Joel Cornett Jan 16 '13 at 01:06
  • 1
    @JoelCornett My impression from "then go to the next line" was that it was a multi-line string. @Duncan? – Alex L Jan 16 '13 at 01:15
  • @AlexL: The fact that, given 492 characters of input, it prints a bunch of 50-character lines and then a 42-character line implies that Joel Cornett's guess is right. Otherwise, that 42 would be a huge coincidence. (Still, it would be nice for the OP to confirm.) – abarnert Jan 16 '13 at 02:40
  • @abarnert Agreed. You're probably right, but I think it's better to have answers for both interpretations of the question until we know either way – Alex L Jan 16 '13 at 02:45
  • @JoelCornett is right. The input is a single string without newlines; it's the output that should have multiple lines. Sorry for the confusion. – Zoë Clark Jan 16 '13 at 02:59
0
In [363]: mystr = "A"*492

In [364]: print '\n'.join(mystr[i*width:(i+1)*width] for i in range(int(math.ceil(float(len(mystr))/width))))
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
0

As @Martijn Pieters implied, this would be trivially easy if you had a way to iterate an iterator by chunks (of n). And if you read that question, you do have a way to do that.

So, given the grouper implementation from the itertools recipes (or anything else you prefer from that other question):

lines = [''.join(group) for group in grouper(50, my_string, '')]

Or, if you just want to print them out:

for group in grouper(50, my_string, ''):
    print ''.join(group)

Once you know that grouper exists, I think this is simpler than Joel Cornett's answer. Note that his didn't work in the first version, and had to be fixed; this one is pretty much impossible to get wrong. Anything that eliminates the possibility of fencepost errors is usually better; that's why we have for-in loops and enumerate instead of C-style for loops, and so on.

Here it is in action:

>>> my_string='1234567890'*49+'12'
>>> print my_string
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012
>>> # That was ugly…
>>> for group in grouper(50, my_string, ''):
...     print ''.join(group)
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
123456789012345678901234567890123456789012
>>> # Pretty!
Community
  • 1
  • 1
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I see it now. It's just that my limited newbie lexicon doesn't include anything like "iterated iterators" ;-) – Zoë Clark Jan 16 '13 at 03:06
  • 1
    @DuncanWadsworth: Yeah, the terminology is a bit hard to figure out if you don't already know what you're looking to figure out. But the concept is simple, once you get it: You have a string with 492 characters, and you want to group them 50 at a time. A string is just a kind of iterable, so a general way to group iterables would give you a way to group strings. All simple—except if you didn't already know about the general iterable concept, you couldn't even think to ask for such a general solution, much less find it on your own. – abarnert Jan 16 '13 at 06:29