我已经采用整数输入并试图在Python中反转它但是徒劳!我把它改成了一个字符串,但我仍然无法做到.有没有办法扭转它?有没有内置功能?
我无法将整数转换为列表,因此无法应用反向函数.
Ada*_*eld 30
您可以使用切片运算符来反转字符串:
s = "hello, world"
s = s[::-1]
print s # prints "dlrow ,olleh"
Run Code Online (Sandbox Code Playgroud)
要将整数转换为字符串,将其反转并将其转换回整数,您可以执行以下操作:
x = 314159
x = int(str(x)[::-1])
print x # prints 951413
Run Code Online (Sandbox Code Playgroud)