Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • How to reverse a string in python
    How do I reverse a string in python?
    Saifuddin posted this question on 1/14/14 | python
    Answers
  • +
  • 9
  • -
  • Go for yourString[::-1]
  • +
  • 1
  • -
  • tooNoob is correct, but you might wan't to read about slicing in python, which is what he used.
  • +
  • 1
  • -
  • def FirstReverse(): str = input("Please enter the text : ") #this line to input any string you need print(str[::-1]) #this line to reverse the string FirstReverse() #this code to run
  • +
  • 0
  • -
  • if you are trying for a for loop try to append the string to an empty list with the last letter of the string the for loop should look like for x in range( len(string)-1): temp.append(string[len(string)-1-x]) also dont forget to use "".join(temp) to remove the "" from the string
  • +
  • 0
  • -
  • data = "I am a coder" chars = [each for each in data] chars.reverse() print ''.join(chars) >>>redoc a ma I
  • +
  • 0
  • -
  • There are actually a few different ways. One of the most famous and easy ones is string[::-1] This example used string slicing and made the step -1, which makes it go backwards. Another, longer way is list(reversed(list(string))) But that is usually used when you have a list. I am sure that there are more, harder choices, but I don't know them all.
    Log in to write an answer.