码迷,mamicode.com
首页 > 编程语言 > 详细

python中将12345转换为'12345',不要使用str

时间:2018-06-11 20:32:19      阅读:186      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!