Python string library does’nt support the in-built “reverse()” as done by other python containers like list, hence knowing other methods to reverse string can prove to be useful. This article discusses several ways to achieve it.
The original string is : Geeksforgeeks The reversed string(using loops) is : skeegrofskeeG
Explanation : In above code, we call a function to reverse a string, which iterates to every element and intelligently join each character in the beginning so as to obtain the reversed string.
The original string is : Geeksforgeeks The reversed string(using recursion) is : skeegrofskeeG
Explanation : In the above code, string is passed as an argument to a recursive function to reverse the string. In the function, the base condition is that if the length of the string is equal to 0, the string is returned. If not equal to 0, the reverse function is recursively called to slice the part of the string except the first character and concatenate the first character to the end of the sliced string.
The original string is : Geeksforgeeks The reversed string(using stack) is : skeegrofskeeG
Explanation : An empty stack is created. One by one characters of string are pushed to stack.
One by one all characters from stack are popped, and put them back to string.
The original string is : Geeksforgeeks The reversed string(using extended slice syntax) is : skeegrofskeeG
Explanation : Extended slice offers to put a “step” field as [start,stop,step], and giving no field as start and stop indicates default to 0 and string length respectively and “-1” denotes starting from end and stop at the start, hence reversing string.
The original string is : Geeksforgeeks The reversed string(using reversed) is : skeegrofskeeG
Explanation : The reversed() returns the reversed iterator of the given string and then its elements are joined empty string separated using join(). And reversed order string is formed.
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Python | Ways to split a string in different ways
- Different ways of sorting Dictionary by Keys and Reverse sorting by keys
- Different methods to reverse a string in C/C++
- Different Ways to Remove all the Digits from String in Java
- Different Ways to Generate String by using Characters and Numbers in Java
- Different ways to access Instance Variable in Python
- Print lists in Python (4 Different Ways)
- Python | Multiply all numbers in the list (4 different ways)
- Extending a list in Python (5 different ways)
- Different ways to clear a list in Python
- Python | Different ways to kill a Thread
- Different ways to Invert the Binary bits in Python
- Different ways to convert a Python dictionary to a NumPy array
- PyQt5 – Different padding size at different edge of Label
- PyQt5 - Setting different toolTip to different item of ComboBox
- Array of Strings in C++ (5 Different Ways to Create)
- Different ways to create Pandas Dataframe
- Different ways to import csv file in Pandas
- Different ways to iterate over rows in Pandas Dataframe
- Create a new string by alternately combining the characters of two halves of the string in reverse