Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a list of integers to string

Tags:

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' 
like image 587
Mira Mira Avatar asked Nov 17 '14 18:11

Mira Mira


People also ask

How do I turn a list of numbers into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

How do you convert a numeric array to a string in Python?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.


2 Answers

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

''.join(map(str,new)) 
like image 142
tynn Avatar answered Sep 29 '22 07:09

tynn


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) 
like image 32
jgritty Avatar answered Sep 29 '22 07:09

jgritty