Reverse a string in Python

前端 未结 28 3353
南旧
南旧 2020-11-21 04:41

There is no built in reverse function for Python\'s str object. What is the best way of implementing this method?

If supplying a very conci

28条回答
  •  长情又很酷
    2020-11-21 04:57

    Sure, in Python you can do very fancy 1-line stuff. :)
    Here's a simple, all rounder solution that could work in any programming language.

    def reverse_string(phrase):
        reversed = ""
        length = len(phrase)
        for i in range(length):
            reversed += phrase[length-1-i]
        return reversed
    
    phrase = raw_input("Provide a string: ")
    print reverse_string(phrase)
    

提交回复
热议问题