There are various ways to reverse a string in Python, however, we will discuss the four most used methods.
- reversed() with slice
- reversed() .join()
sli = "Hi Universe"[::-1] print(sli)
It is a simple example above we used the slice method to reverse the string, in the above example the “[::-1]” means that it will create a slice from the end of the string and stop at the end.
The “-1” denotes the position of the last character in the string that will go backward for printing the string in reversed form.
As you can see from the output the slice method reversed the string.
Reversed with slice
def reverse(h): # using extended slicing here h = h[::-1] return h # reversing the string h = "how to reverse a string" # Printing the actual string print("the actual string was:", h) # Printing the reversed string print("the reversed string is:", reverse(h))
In the above example, we used the reserve method with extended slicing, first of all, we defined the reverse method and passed a string as a parameter ‘h’ then we used the slicing for reversing the string after that we assigned a string to the parameter value ‘h’ and then we just printed the string.
the actual string was: how to reverse a string the reversed string is: gnirts a esrever ot who
Reversed with join
#original string p = 'reverse string in python' reversed=''.join(reversed(p)) print(reversed)
In the above example, we used reversed.join() method. The .join() method combines all characters from the output of reversed string in an updated string.
nohtyp ni gnirts esrever
As you can see the output reversed the whole string.
def reverse(p): # string will be returned if the length is 0 if(len(p)==0): return p # Else cutout the last character of string and merge it with string else: return p[-1] + reverse(p[:-1]) # String to reverse p = "this is the reverse method for string" # Printing the original string print(" the actual string was :", p) # Printing the reversed string print("the Reversed string is :", reverse(p))
in the above example, we applied the recursion on reversed method, reversed method will run recursively until the string becomes reversed.
It will check if the length of the string is equal to 0 so the string will be returned, otherwise, the reversed method will recursively trim the string and will combine it with the starting of the trimmed string. After applying the technique we just printed the reversed string.
the actual string was : this is the reverse method for string the Reversed string is : gnirts rof dohtem esrever eht si siht
In this tutorial we discussed the methods for reversing a string in python, we discussed a total of four ways to reverse a string.
Firstly we practiced an example using the slice method then we discussed the example of a reversed method with an extended slice, we also discussed the example of a reversed method with join, and then we saw the recursion method for reversing the string recursively.
Hope you like the tutorial if you have any queries so feel free to ask below, Thanks for reading.