In this concept, we have 8 different ways to reverse a string using python. Most commonly used methods are explained below:
- Using the slicing method
- Using the reversed() function
- Using For loop
- Using the Recursion
- Using join() and list comprehension method
- Using the reduce() function
- Using the stack
- Using the list comprehension
- Using the while loop
Reversing a string using slicing method
The best way to reverse a string using string indexing, with a step of -1.
def reverse_string(s): return s[::-1] print("Reverse of the string is : ", reverse_string("Python")
Output : nohtyP