Reverse string program in Python
Written by
In python, string refers to the character of data.
To reverse a string, as such there is no definite way either we can use slicing or we can implement the code by loop method where we will be storing the string in reverse order by decrementing the index.
For example:
input: himani
output: inamih
input: kohli
output: ilhok
Here, we will be learning to reverse a string using python programming.
Algorithm:
- Input the string from the user by using input() function.
- Using a function string[::-1] refers to reverse a string and store it into a variable named revstring.
- Revstring variable stores the reverse of a string.
- Print the variable revstring.
- Reverse of the string is printed as a result.
- Exit
Code:
string=input("Enter a string:")
revstring=string[::-1] //performs the reverse of the string
print(revstring) // print the reversed string
Output:
Enter a string: studymite
etimyduts