Generate string of numbers python

◇◆丶佛笑我妖孽 提交于 2019-12-04 03:11:10

问题


Hello I want to create a list of strings where each string is a number.

Fox example given the number 4 I would like to create a function that returns a list with elements '0','1','2','3','4'. In C/C++ this can be done by using the Ascii code of 0 and then increase them. I am new to python and I don’t know how to do it. Is there any way to do it?


回答1:


You can do this with str and range like this

map(str, range(number + 1))

When number = 4,

print(map(str, range(4 + 1)))
# ['0', '1', '2', '3', '4']



回答2:


map being frowned upon by many python developers, here is the comprehension list equivalent:

 [str(x) for x in range(my_value + 1)]


来源:https://stackoverflow.com/questions/22044220/generate-string-of-numbers-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!