Reverse a string in Python

前端 未结 28 2470
南旧
南旧 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 05:03

    With python 3 you can reverse the string in-place meaning it won't get assigned to another variable. First you have to convert the string into a list and then leverage the reverse() function.

    https://docs.python.org/3/tutorial/datastructures.html

       def main():
            my_string = ["h","e","l","l","o"]
            print(reverseString(my_string))
    
        def reverseString(s):
          print(s)
          s.reverse()
          return s
    
        if __name__ == "__main__":
            main()
    

提交回复
热议问题