There is no built in reverse
function for Python's str
object. What is the best way of implementing this method?
If supplying a very concise answer, please elaborate on its efficiency. For example, whether the str
object is converted to a different object, etc.
Here is simply:
print "loremipsum"[-1::-1]
and some logically:
output:
muspimerol
A lesser perplexing way to look at it would be:
In English [-1::-1] reads as:
using slice notation
using reversed() function
using recursion
Sure, in Python you can do very fancy 1-line stuff. :)
Here's a simple, all rounder solution that could work in any programming language.
Reverse a string in python without using reversed() or [::-1]
Here is one without
[::-1]
orreversed
(for learning purposes):you can use
+=
to concatenate strings butjoin()
is faster.