+ 1
Any way to generate all the numbers from 0 to 9 individually in string format and then saving in a variable?
I want variable=('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') but don't want to write it.
5 ответов
+ 6
Use range(10) function.
And map to str, Make returned iterator into tuple and print it. Can you try it?
edit:
x = tuple( map(str, range(10)) )
+ 6
ar = tuple(str(item) for item in range(10))
+ 3
x = tuple('0123456789')
However, I'm wondering now why you need that.
What's the difference between a string and the very same letters strung up as a tuple?
+ 2
import string
x = tuple(string.digits)
print(x)
The string module also have other convenient constants and methods.
https://www.digitalocean.com/community/tutorials/JUMP_LINK__&&__python__&&__JUMP_LINK-string-module
0
Yes, you can use Python's built-in map() function to convert a range of numbers from 0 to 9 to strings, and then join them into a single string separated by commas. Here's an example:
numbers = list(map(str, range(10)))
variable = ", ".join(numbers)
print(variable)