How to create a list of string using range() built-in function in python?
range(1,7) produces a range object equivilant to [1,2,3,4,5,6]
Desired list: ['1','2','3','4','5','6']
Use a cast to str() & list comprehension as per the following:
string_list = [str(x) for x in range(1, 7)]
With the map function:
list(map(str,range(7)))
[*map(str,range(7))]. But your solution is much more readable.Or the new f-strings:
>>> [f'{i}' for i in range(7)]
['0', '1', '2', '3', '4', '5', '6']
Use string formatting & list comprehension as per the following
string_list = ["{}".format(x) for x in range(1,7)]
str.format here - str(x) would have worked just as well as "{}".format(x). Also, if you wanted to start from 1, your range call is wrong.str.format here; just call format(x, "03") instead of "{:03}".format(x). You only need str.format to put multiple things together. And even then, f-strings are usually better: f"{x:03}".range call. and you are right about the use of str(x) it is also a solution
range(7)does not output[1,2,3,4,5,6].