3

How to create a string from an iterator over string in Python?

I am currently just trying to create a reversed copy of a string, I know I could just use slice:

s = 'abcde'
reversed_s = s[::-1]

I could also create a list from the iterator and join the list:

s = 'abcde'
reversed_s_it = reversed(s)
reversed_list = list(reversed_s_it)
reversed_s = ''.join(reversed_list)

But when I try to create a string from the iterator directly, it just gives a string representation of the iterator, if I do this:

s = 'abcde'
reversed_s_it = reversed(s)
reversed_s = str(reversed_s_it)

reversed_s will give a string representation of the iterator instead of iterating the iterator to create a string.

print(reversed_s)

Output

<reversed object at 0x10c3dedf0>

Furthermore

print(reversed_s == 'edcba')

Output

False

Therefore, I just want to know if there is a Pythonic way to create a string from an iterator?

SiAce
  • 357
  • 3
  • 15
  • What's wrong with `''.join(reversed(s))`? – chepner Dec 25 '20 at 22:30
  • @chepner, I just don't feel good about using an extra space for creating an extra list to construct a string, I'm not sure if there is a better way – SiAce Dec 25 '20 at 22:33
  • Iterators can be materialized as lists or tuples by using the list() or tuple() constructor functions. So it is hard to imagine there’s another way to skip this step. – Daniel Hao Dec 25 '20 at 22:33
  • String is an iterable object in python. and type(reversed_s) shows it as a str object what do you mean when you say "reversed_s is still an iterator" – Sandeep Polamuri Dec 25 '20 at 22:36
  • "reversed_s is still an iterator." no, it isn't. It's a `str` object. In any case, you can do `''.join(reversed(s))` **but** that will still create a list underneath the hood, but it is "directly" from an iterator. – juanpa.arrivillaga Dec 25 '20 at 22:41
  • @SandeepPolamuri, sorry, it seems I am wrong and `reversed_s` is a string, but when I try to print it, it shows as `` – SiAce Dec 25 '20 at 22:42
  • @juanpa.arrivillaga, I just found out that I was wrong, but when when I try to print it, it shows as ``, why is that? – SiAce Dec 25 '20 at 22:44
  • 1
    @SiAce because that's the string representation of that iterator, which is what you asked for when you did `str(reversed_s_it)`, the same as any object, which just happens to be the default `__str__` implementation inherited from `object`, so, for example, `class Foo: pass` then `print(Foo())` will give you something similar. – juanpa.arrivillaga Dec 25 '20 at 22:47
  • @SiAce print(type(reversed_s)) shows as in both python2 and python3 compilers. – Sandeep Polamuri Dec 25 '20 at 22:47
  • @juanpa.arrivillaga, I see, now I understand that the reversed_s is a string whose content is ``. I was confused at first. – SiAce Dec 25 '20 at 23:06
  • Does this answer your question? [Reverse a string in Python](https://stackoverflow.com/questions/931092/reverse-a-string-in-python) – Georgy Dec 28 '20 at 14:31

1 Answers1

3

''.join itself only needs an iterable; you don't have to create a list first.

>>> ''.join(reversed(s))
'edcba'

However, there's nothing wrong with s[::-1]; in fact, it's quite a bit faster than using ''.join to concatenated the elements from the reversed object.

chepner
  • 497,756
  • 71
  • 530
  • 681