How do I convert an iterator over bytes into a str?

John Machin sjmachin at
Wed Aug 19 04:32:14 CEST 2009

On Aug 19, 8:24 am, markscottwright <markscottwri... at> wrote:
> This does what I expected:
>     In [6]: list(iter([1,2,3,4,5]))
>     Out[6]: [1, 2, 3, 4, 5]
> But this appears to be doing a __repr__ rather than making me a nice
> string:
>    In [7]: str(iter("four score and seven years ago"))
>    Out[7]: '<iterator object at 0x0139F190>'
> What's the correct way to turn an iterator over bytes into a string?
> This works, but, ewww:
>     In [8]: "".join(iter("four score and seven years ago"))
>     Out[8]: 'four score and seven years ago'

There is no such thing as an "iterator over bytes" in Python 2.x.
There is no such concept as "convert an iterator over <anything> into
a str" object.

What you have is an iterator over str objects of length 1. To do what
you appear to actually want to do (concatenate a bunch of strings), it
is recomemnded to use ''.join(str_iterable).

More information about the Python-list mailing list