Rotating strings in Python

后端 未结 5 703
面向向阳花
面向向阳花 2020-12-03 22:51

I was trying to make the string HELLO to OHELL in Python. But couldn\'t get any way to rotate it without working with loops. How to code for it in

5条回答
  •  醉梦人生
    2020-12-03 23:12

    Here is what I use to rotate strings in Python3:

    To rotate left by n:

    def leftShift(text,n):
        return text[n:] + text[:n]
    

    To rotate right by n:

    def rightShift(text,n):
        return text[-n:] + text[:-n]
    

提交回复
热议问题