TutorialStudyMite
Reverse string program in Python
HHimani Kohli1 min read
Beginner friendly
Track completion, mastery, and revision.
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 stringOutput:
Enter a string: studymite
etimydutsFinished reading?