13

I want to convert the integers from 0 to 9 to strings. I was able to do it by manually transforming each number like this:

str(1) = a
str(2) = b

... All the way untill 9. However, this is slow and the code doesn't look very pythonic. I would like to see a faster to code solution, such as putting all these numbers into a list and then transforming each element inside the list to a string. I know that to make said list I should do it like this:

a = range(0,10)

However, I don't know how to transform the ints inside the list to strings. Thanks in advance for your help.

3
  • SO each number will correspond to a letter like 1 = a, 2 = b?
    – Beginner
    Commented Oct 11, 2014 at 23:10
  • Kinda. If you write str(write your integer here) your integer will switch from the int data type to the str data type. I just assign that number that belongs to the str type to a variable named a or b or whatever you want to call it, as long as you follow the naming rules, so that I can use it later in my program Commented Oct 11, 2014 at 23:22
  • @Beginner - no. a,b, etc are variables. (you can tell because they are not quoted and they're not numbers) So they stand for something. In this case, we assume from the title that they stand for integers. The str() function coerces a variable to present itself in a string representation - what that looks like depends on the variable type, but for integers, it's just the decimal representation, presented as a string. Notice that 1 != "1", as in most languages. (try evaluating "1" +"1") Commented Oct 12, 2014 at 0:52

6 Answers 6

27

You can use map() to apply str() to each value in your array:

a = map(str, range(0, 10))
1
  • 2
    if you want a list you have to pass the map to list(). e.g.: list(map(f, a)) Commented May 20, 2021 at 18:36
25

Use a list comprehension:

[str(i) for i in range(10)]
1
  • Or a generator expression - it's lazy: (str(I) for I in range (10)) . List comprehensions are eager. Commented Oct 11, 2014 at 23:40
6

You can use map

a = range(0,10)
print(list(map(str,a))) # <- python 3
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

a = xrange(0,10)
print(map(str,a)) # <- python 2
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
2
  • Thanks, extra points for explaining it in python 2 and 3. Gosh, in python 3 it is worse. Also, you don't need xrange, you can simply write range Commented Oct 11, 2014 at 23:26
  • @pedromatias, in python3 range is an iterator but not in python 2 so using xrange is better in python2 Commented Oct 11, 2014 at 23:30
0

Here is another answer:

a = map(chr, range(48, 58))
print a
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

Here is the docstring for chr which explains what I did:

Docstring:
chr(i) -> character

Return a string of one character with ordinal i; 0 <= i < 256

If you simply called chr(48) you will get '0' , chr(49)='1' and so on ...

0

If you need to convert the list of ints to a string of strings, you can do this:

a = range(0,10)
print "'" + "','".join((map(str,a))) + "'"
'0','1','2','3','4','5','6','7','8','9'

print type("'" + "','".join((map(str,a))) + "'")
<type 'str'>

This can be useful if you have many integer types that need to be queried as string types in a separate database, for example.

0
n = [0,1,2,3,4]
n = map(str,n)
n = list(n)
print(n)

Output:
n = ['0','1','2','3','4']
2
  • Please add some explanation
    – mousetail
    Commented Jun 30, 2021 at 7:07
  • This answer is not different than any other posted above.
    – ARAT
    Commented Feb 6, 2022 at 13:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.