Python Program to Reverse a String without using Recursion


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 −

 Live Demo

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.

Updated on: 12-Mar-2021

423 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements