When it is required to reverse a string without using recursion technique, simple negative indexing can be used.
Indexing helps the values to access the elements at a specific index.
Example
Below is a demonstration for the same −
my_string = str(input("Enter a string that needs to be reversed: ")) print("The string after reversal is: ") print(my_string[::-1])
Output
Enter a string that needs to be reversed: Jane The string after reversal is: enaJ
Explanation
- The string input is taken by the user.
- This value is assigned to a variable.
- It is displayed on the console.
- It is negative indexed and displayed on the console.