There is no built in reverse
function in Python's str
object. What is the best way of implementing this?
If supplying a very concise answer, please elaborate on it's efficiency. Is the str
converted to a different object, etc.
How about:
This is extended slice syntax. It works by doing |
|||||||||||||||||||||
|
@Paolo's |
|||||||||||||||||||||
|
In my long illustrious career of being a programmer, I have never seen a practical need to reverse a string. So congratulations, you must be learning Python. There's a couple of things about Python's strings you should know:
The subscript creates a slice by including a colon within the braces:
To create a slice outside of the braces, you'll need to create a slice object:
A readable approach:While
Most performant approach:Much faster is using a reverse slice:
But how can we make this more readable and understandable to someone less familiar with the intent of the original author? Let's create a named slice object, and pass it to the subscript notation.
Implement as FunctionTo actually implement this as a function, I think it is semantically clear enough to simply use a descriptive name:
And usage is simply:
What your teacher probably wants:If you have an instructor, they probably want you to start with an empty string, and build up a new string from the old one. You can do this with pure syntax and literals using a while loop:
This is theoretically bad because, remember, strings are immutable - so every time where it looks like you're appending a character onto your Best PracticeTheoretically better is to collect your substrings in a list, and join them later:
However, as we will see in the timings below for CPython, this actually takes longer, because CPython can optimize the string concatenation. TimingsHere are the timings:
CPython optimizes string concatenation, whereas other implementations may not:
|
||||
|
Quick Answer (TL;DR)Example
Detailed AnswerBackgroundThis answer is provided to address the following concern from a user odigity:
Problem
Solution
Pitfalls
RationalePython has a special circumstance to be aware of: a string is an iterable type. One rationale for excluding a In simplified terms, this simply means each individual character in a string can be easily operated on as a part of a sequential array of elements, just like arrays in other programming languages. To understand how this works, reviewing example02 can provide a good overview. Example02
ConclusionThe cognitive load associated with understanding how slice notation works in python may indeed be too much for some adopters and developers who do not wish to invest much time in learning the language. Nevertheless, once the basic principles are understood, the power of this approach over fixed string manipulation methods can be quite favorable. For those who think otherwise, there are alternate approaches, such as lambda functions, iterators, or simple one-off function declarations. If desired, a developer can implement her own string.reverse() method, however it is good to understand the rationale behind this "quirk" of python. See also |
||||
Reverse a string in python without using reversed() or [::-1]
|
|||||||||||||
|
Here is a no fancy one:
|
|||
|
|
|||||
|
Here is one without
you can use |
|||
|
A lesser perplexing way to look at it would be:
In English [-1::-1] reads as:
|
|||
|
Sure, in Python you can do very fancy 1-line stuff. :)
|
|||
|
You can use the reversed function with a list comprehesive. But I don't understand why this method was eliminated in python 3, was unnecessarily.
|
|||||||||
|
OUTPUT :
|
||||
|
|
|||||||||||||
|
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
''.join((s[i] for i in xrange(len(s)-1, -1, -1)))
. – Dennis Mar 6 '13 at 6:49