swap letters in a string in python

后端 未结 3 941
广开言路
广开言路 2021-01-12 10:43

I am trying to switch the first character in a string and move it to the end of the string. It needs to repeat the rotation a number of n times. For example, rotateLef

3条回答
  •  没有蜡笔的小新
    2021-01-12 10:58

    I know this is old, but you can use collections.deque:

    from collections import deque
    
    s = 'Impending doom approaches!'
    d = deque(s)
    d.rotate(11)
    print(''.join(d))
    >>> approaches!Impending doom
    d.rotate(-21)
    print(''.join(d))
    >>> doom approaches!Impending
    

提交回复
热议问题