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:

  1. Input the string from the user by using input() function.
  2. Using a function string[::-1] refers to reverse a string and store it into a variable named revstring.
  3. Revstring variable stores the reverse of a string.
  4. Print the variable revstring.
  5. Reverse of the string is printed as a result.
  6. 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

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019