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?