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.
Here, we will be learning to reverse a string using python programming.
- 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.
string=input("Enter a string:") revstring=string[::-1] //performs the reverse of the string print(revstring) // print the reversed string
Enter a string: studymite etimyduts