Python has no in-built function to reverse a string. Thus, we need to implement our own logic to reverse a string.
We will reverse a string using different methods.
Using FOR Loop
The idea behind this method is to use a reverse loop starting from the last index of the string to the 0th index. At each iteration, we will be adding characters of the string to hold the reversed string at the end of the iterations.
Let’s look on the general procedure we will be following −
a:=new string
loop from last index of string to 0th index and decrement by 1 a=a+character at the current index
return a
Example
def reverse(s): a="" last_index=len(s)-1 for i in range(last_index,-1,-1): a=a+s[i] return a string="TutorialsPoint" print(reverse(string))
Output
INPUT : TutorialsPoint OUTPUT : tnioPslairotuT
Intelligent implementation of the above approach is as follows −
Here, we use for each loop and append each character at the beginning of the new string.
Appending each character of the new string gives us the reversed string.Instead of for each loop ,we can use the normal loop from index 0 to length of the string.
Example
def reverse(s): a="" for i in s: a=i+a return a string="TutorialsPoint" print(reverse(string))
Output
INPUT : TutorialsPoint OUTPUT : tnioPslairotuT
Using While Loop
We can replace for loop with while loop.We will iterate while loop from last index to 0th index and concatenate each character at current index to the end of the new string to obtain the reversed string.
Example
def reverse(s): a="" i=len(s)-1 while(i>=0): a=a+s[i] i-=1 return a string="TutorialsPoint" print(reverse(string))
Output
INPUT : TutorialsPoint OUTPUT : tnioPslairotuT
Using Extended Slice syntax
The extended slice syntax has three parameters, [starts,end,step]. Giving no fields as start and end by default means 0 as start and length of the string as end.Specifying step as ‘-1’ means starting from the end and stop at the start.Hence, we get the reversed string.
Example
def reverse(s): a=s[::-1] return a string="TutorialsPoint" print(reverse(string)) Using join and reversed()
Output
INPUT : TutorialsPoint OUTPUT : tnioPslairotuT
We can reverse the function using reversed(). But reverse function returns the reversed iterator. Therefore we need to join the characters of the reversed iterator to obtain a string.
Using join and reversed()
Example
def reverse(s): a="".join(reversed(s)) return a string="TutorialsPoint" print(reverse(string))
Output
INPUT : TutorialsPoint OUTPUT : tnioPslairotuT
Using Recursion
We can reverse a string using recursion.We will recursively call the reverse function passing the string from index 1 to last. Thus,we will be continuously reducing the length of the string towards 0 which is our base case to return the string. Thus, at each recursive call we slice the string from index 1 to last and concatenate the first character at the end of the sliced string.
Example
def reverse(s): if(len(s)==0): return s else: return reverse(s[1:])+s[0] string="TutorialsPoint" print(reverse(string))
Output
INPUT : TutorialsPoint OUTPUT : tnioPslairotuT
These were some of the methods to reverse a string in python. We have discussed five different methods to reverse a string in Python.
Which is the fastest and best method to use?
The fastest and best method to use to reverse a string is using Slicing (Method 3). It gives the fastest results among all the methods to reverse a string in python. This method is very easy to implement as it has just 1 line of code and we need not implement any logic from our side.