3 ms·
Or you could use the "reversed" built-in function: s = "string" reversed(s) It also has the plus of creating a generator, saving memory from a new array a
by BoppreH 14y ago
Or you could use the "reversed" built-in function:
s = "string"
reversed(s)
It also has the plus of creating a generator, saving memory from a new array allocation.
- wting 14y ago`reversed(s)` returns an iterator. The much slower equivalent of `s[::-1]` is `''.join(reversed(s))`. It's odd how Python has list.reverse() but not str.reverse(). Probably a conscious design decision sometime ago.
- ot 14y agolist is mutable while str is not. So reverse() should return a new copy, but it would duplicate the functionality of s[::-1] (which is quite idiomatic, although a bit obscure)