String Reverse in Python
Introduction
When we use strings in our python code, we can come across several conditions where we have to reverse the string, but we don't have any inbuilt function for reversing the string. In that case, we can use many approaches that can be useful in reversing the string, like using extended slice syntax, looping, recursion, stack, and reversed() function.
How to Reverse a String in Python?
There can be a lot of situations while coding where you have to reverse a string to make use of it. So for that, knowing these string reversal techniques becomes necessary to make the tasks easier. We don't have any inbuilt function for reversing the string. So we follow different approaches for string reversal as listed below:
- Reversing String using extended slice syntax
- Reversing String using loop
- Reversing String using recursion
- Reversing String using stack
- Reversing Strings With .join() and reversed()
Let's take a look at all these methods to reverse a string.
Using extended slice syntax
We can make use of python's slicing technique to reverse a string. The string can be extended sliced using the following syntax:
Where the start is the starting index by which substring is made, the end is the end index of the string, and step is the integer value that determines the increment between each integer in the string's start and end index. If the step is set to -1, it accesses the string in reverse order.
We will put starting and ending as empty to access the whole string length and the step as -1 to access the string in reverse order. Hence, we will get the whole string in reversed order. In this process, reversed version of the given string is created and assigned to our original string.
The Time Complexity of this approach is where is the length of the string to be reversed.
Example:
Output:
Using Loop
We can reverse a string by iterating the string through a reversed loop from the end of the string to the starting of the string and concatenating each character to a new string. In this process, the new reversed string is created and printed. This method is generally slow because of the loop, as well as the copy we create each time we add a character to the string. The Time Complexity of this approach is where is the length of the string to be reversed.
Example:
In this example, we are iterating the string in reverse order by setting the start value of the loop to . And end to -1, and step to -1 to iterate from the string length to the index.
After iterating in reverse order, we concatenate the values to a new string variable.
Output:
Using Recursion
We can use the concept of recursion to reverse the string. We will recursively call the reverse() function and keep adding the last index character to the starting of the string. Let's take an example of a string "ABC". And for that, the string is passed as an argument to the recursive function, which reverses the string. The function's base condition is that the string is returned if its length equals zero. If not equal to 0, the reverse function recursively slices the string except for the last character and concatenates the last character to the start of the string. Below is the recursion tree for the string "ABC".
The Time Complexity of this approach is where is the length of the string to be reversed.
Example:
The string is passed as an argument to the recursive function in the code below, which reverses the string. The function's base condition is that the string is returned if its length equals zero. If not equal to 0, the reverse function recursively slices the string except for the last character and concatenates the last character to the start of the string. In this process, new string will not be created as we are making changes in the given string itself.
Output:
Using Stack
We can make use of a stack to reverse our string. We will use a stack's push and pop functions to push all the characters to the stack and then pop them one by one to get the whole string in reverse order.
Below is the flowchart of an example string "ABC" reversed using stacks.
The Time Complexity of this approach is where is the length of the string to be reversed.
Example:
A new stack is constructed. String characters are pushed to the stack one by one. All characters from the stack are popped and returned to the string. So in this process, the new reversed string is created and assigned to our original string.
Output:
Reversing Strings With .join() and reversed()
reversed() function in python is used to get the reverse of the given iterator in the form of a list. .join() function concatenates all the characters of the iterator into a new original string. We can use the reversed() function to get the reverse of the string in a list and then use the .join() function to concatenate all the characters to our original string.
Time Complexity of this approach is O(N) where N is the length of the string to be reversed.
Example:
The reversed() function returns the reversed list of the provided string. And its elements are then merged using join() to form a reversed string. The old string replaces the newly reversed string by assignment operation.
Output:
Conclusion
- We don't have any inbuilt function for reversing the string. So we follow different approaches for string reversal:
- Reversing String using extended slice syntax
- Reversing String using loop
- Reversing String using recursion
- Reversing String using stack
- Reversing Strings With .join() and reversed()