在Python中怎么互换个位与十位的位置
可以将整数转化为字符串,然后通过字符串的切片操作来交换个位和十位的位置,最后再将字符串转化为整数。
示例代码:
n = 123
s = str(n)
if len(s) == 1:
result = n
else:
result = int(s[1] + s[0] + s[2:])
print(result)
输出结果为:
213
原文地址: https://www.cveoy.top/t/topic/fpiE 著作权归作者所有。请勿转载和采集!