标签:style while ast int pytho range 字符 nbsp bsp
a = 12345
#创建一个空字符串
ret = ""
#whlie循环,条件为当a为true时,即a不是 0的时候
while a :
#定义一个变量,对a求余
last = a%10
#关键一步,把数字变成字符赋值给ret
ret = ret + chr(ord("0")+last)
# a求余取整
a = a//10
#打印ret
print(ret)
当然啦,我们用for循环也可以做这道题,不过遗憾的时,会用到str
a = 12345
b=len(str(a))
ret = ""
for x in range(b):
last = a%10
ret = ret + chr(ord("0")+last)
a = a//10
print(ret)
python中将12345转换为'12345',不要使用str
标签:style while ast int pytho range 字符 nbsp bsp
原文地址:https://www.cnblogs.com/chaojiyingxiong/p/9168656.html