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, rotateLeft(hello,2)=llohe.
I tried doing
def rotateLeft(str,n):
rotated=""
rotated=str[n:]+str[:n]
return rotated
Is this right, and how would you do it if it remove the last character and move it to the front of the string?
You can shorten it to
and simply use negative indices to rotate “to the right”:
If you want to keep rotating even after exceeding the length of the string, use
so you get