swap letters in a string in python

后端 未结 3 960
广开言路
广开言路 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:46

    The only problem with the code you have posted is the fact that you're trying to use "str" as a name for a string. This is the name of a built in function in Python, and that's why you're getting errors. See more: http://docs.python.org/library/functions.html#str You cannot use that as a name for something.

    Changing the name of the string you pass rotateLeft will solve your problem.

    def rotateLeft(string,n):
        rotated=""
        rotated=string[n:]+string[:n]
        return rotated
    

提交回复
热议问题