how to loop from 0000 to 9999 and convert the number to the relative string?(如何从 0000 循环到 9999 并将数字转换为相对字符串?)
问题描述
我想得到一些字符串,范围从0000到9999,也就是说,我想打印以下字符串:
i want to get some string, range from 0000 to 9999, that's to say, i want to print the following string:
0000
0001
0002
0003
0004
0005
0006
....
9999
我尝试使用 print "
".join([str(num) for num in range(0, 9999)])
,但失败了,我得到以下数字:
i tried to use print "
".join([str(num) for num in range(0, 9999)])
, but failed, i get the following number:
0
1
2
3
4
5
6
...
9999
我希望python自动添加前缀0,使数字保持4位数字时间.谁能帮帮我?任何帮助表示赞赏.
i want python to add the prefix 0 automatically, making the number remain 4 bit digits all the time. can anyone give a hand to me? any help appreciated.
推荐答案
获得所需内容的一种方法是使用字符串格式:
One way to get what you want is to use string formatting:
>>> for i in range(10):
... '{0:04}'.format(i)
...
'0000'
'0001'
'0002'
'0003'
'0004'
'0005'
'0006'
'0007'
'0008'
'0009'
所以要做你想做的,这样做:
So to do what you want, do this:
print("
".join(['{0:04}'.format(num) for num in range(0, 10000)]))
这篇关于如何从 0000 循环到 9999 并将数字转换为相对字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 0000 循环到 9999 并将数字转换为相对字符串?


基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01