38

I want to convert my list of integers into a string. Here is how I create the list of integers:

new = [0] * 6
for i in range(6):
 new[i] = random.randint(0,10)

Like this:

new == [1,2,3,4,5,6]
output == '123456'
Georgy
14k7 gold badges69 silver badges80 bronze badges
asked Nov 17, 2014 at 18:21
1
  • new = [ random.randint(0,10) for i in range(6) ] would be simpler. Commented Nov 17, 2014 at 19:22

6 Answers 6

76

With Convert a list of characters into a string you can just do

''.join(map(str,new))
answered Nov 17, 2014 at 18:30
Sign up to request clarification or add additional context in comments.

2 Comments

Or for the map-averse, ''.join([str(x) for x in new]).
Or even ''.join(str(random.randint(0, 10)) for i in range(10))
15

There's definitely a slicker way to do this, but here's a very straight forward way:

mystring = ""
for digit in new:
 mystring += str(digit)
answered Nov 17, 2014 at 18:26

Comments

11

Two simple ways of doing this:

"".join(map(str, A))
"".join(str(a) for a in A)
answered Jul 20, 2019 at 12:57

Comments

1

If you don't like map():

new = [1, 2, 3, 4, 5, 6]
output = "".join(str(i) for i in new)
# '123456'

Keep in mind, str.join() accepts an iterable so there's no need to convert the argument to a list.

answered Oct 18, 2021 at 17:51

Comments

0

Coming a bit late and somehow extending the question, but you could leverage the array module and use:

from array import array
array('B', new).tobytes()
b'\n\t\x05\x00\x06\x05'

In practice, it creates an array of 1-byte wide integers (argument 'B') from your list of integers. The array is then converted to a string as a binary data structure, so the output won't look as you expect (you can fix this point with decode()). Yet, it should be one of the fastest integer-to-string conversion methods and it should save some memory. See also documentation and related questions:

https://www.python.org/doc/essays/list2str/

https://docs.python.org/3/library/array.html#module-array

Converting integer to string in Python?

answered May 13, 2015 at 18:44

2 Comments

Maybe python 3 only? tobytes not available for me in python 2.
This doesn't appear to work. Try getting the output to match the OP's desired output of '123456'
-4

You can loop through the integers in the list while converting to string type and appending to "string" variable.

for int in list:
 string += str(int)
answered Oct 23, 2018 at 5:37

8 Comments

Its totally different
in what way, can you please help me.
@Cherry your code is exactly the same, changing variable names does not make the code different
Plus, this is very bad practice. Never use reserved names as variable names.
@WhatsThePoint Its different, its just that you cannot see
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.