How to reverse strings in Python
- There is no specific method of the str object to reverse a string in Python like the list reverse method, you may use a number of techniques to reverse a string.
- One of the ways is using the extended slice.
- The second way the is reversed() method with join() for reversing a string.
- You may also use the slice object for reversing a given string.
- A loop can also be used to reverse a string.
- The following section explains how to reverse string with code snippets.
First method of Python reverse string: The extended slice
The extended slice is a technique that enables you to slice the data of objects like strings in Python programs. It takes three values i.e.
The object can be a string, list, tuple, etc.
For our topic of this tutorial, reversing a string, we will use the string object.
An example of completely reversing a string
To completely reverse a string by using the extended slice technique, just provide the step value as -1 and leave the other two values as follows:
#Reversing a string example str_reverse = "Hello, This is Python Lesson" print ("The reversed string: ", str_reverse[::-1])
Output:
In the output graphic, you can see the order of the given string is reversed completely.
Reversing only a part of the string
If you require reversing only a part of the string then you may specify the start, end, and step parameters for setting the position within the string.
The negative values for all will play the trick.
See this example where only the word “Python” is reversed from the above example string:
#Reversing a string partially example str_reverse = "Hello, This is Python Lesson" print ("The partial reversed string: ", str_reverse[-8:-15:-1])
Output:
Using slice objects for reversing a string
You may also use the slice objects for reversing the string objects. The slice object is different from the extended slice. It uses the following syntax:
You may learn more about both and see the difference here.
The slice object uses the parenthesis and values are separated by commas. See an example below for reversing a string by using the slice object:
#Reversing a string by slice object str_reverse = "slice object for reversing a String" str_slice = slice(-1,-39, -1) print("Original String:" ,str_reverse) print("Reversed after slice object:" ,str_reverse[str_slice])
Result:
Reversed after slice object: gnirtS a gnisrever rof tcejbo ecils
Using reversed function with join()
Another way of reversing the string is using the reversed function with join(). The reversed() takes a sequence as argument and returns a reversed iterator.
This iterator (string in our case) is used in the join function that concatenates the string by a given sequence.
See a demo below of reversing a string by using these two functions:
#Reversing a string by reversed and join functions str_src = "Reversed and join" reversed_str = ''.join(reversed(str_src)) print("Original String:" ,str_src) print("String after revsered with join:" ,reversed_str)
Output:
The Final Word
Of all the above solutions to reverse strings, the recommended is using the extended slice. This is more efficient and straightforward.
You may try other solutions like using a while loop or creating your own function for academic purposes.