Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate string of numbers python

Tags:

python

numbers

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?

like image 361
JmRag Avatar asked Feb 26 '14 14:02

JmRag


People also ask

How to generate random number string in Python?

Python has a module that is the random module that can be used to generate random number string using its various methods. The random is one of the Modules to get the data by the system itself based on the system logic. Random Module is basically used in the One Time Password (OTP) generation, and in some Games to choose some random Decisions.

How to create a string in Python?

Python does not have a character dtype, a character is simply a string with a length. In this example we can simply use the single or double quotes to create a string and to assign a string to a variable we can simply use the equal to the operator with a string value. Let’s take an example to check how to create a string in python

How to convert list of characters into string in Python?

Here, we will use the Python join (iterable) function to convert list of characters into string. The join (iterable) method is useful to create a string from the iterable sequence characters. This method returns a new string and joins every single character which is present in the given list.

How to get the digit out of a string in Python?

This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.


2 Answers

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

 [str(x) for x in range(my_value + 1)]
like image 144
njzk2 Avatar answered Sep 18 '22 10:09

njzk2


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']
like image 29
thefourtheye Avatar answered Sep 20 '22 10:09

thefourtheye