你可以使用:
my_string = 'hello'
my_string = my_string[-1] + my_string[:-1]
print(my_string)
Run Code Online (Sandbox Code Playgroud)
产量
ohell
Run Code Online (Sandbox Code Playgroud)
my_string[-1]选择字符串('o')中的最后一个字符,并my_string[:-1]选择除最后一个字符('hell')之外的所有字符串.
要移动x金额,您可以使用:
my_string = my_string[-x:] + my_string[:-x]
Run Code Online (Sandbox Code Playgroud)
my_string[-x:]选择字符串中的最后x个字符,并my_string[:-x]选择除最后x个字符之外的所有字符串.