Python Program to Reverse a String without using Recursion

PythonServer Side ProgrammingProgramming

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.
raja
Published on 12-Mar-2021 12:35:07
Advertisements