"Opposite" of splitting?

J

Jan Danielsson

Hello all,

I have a list of integers:

q = [ 1, 2, 4, 7, 9 ]

which I would like to convert to a string:

"1,2,4,7,9"

This is *very* easy to do with a simple while loop.. But I suspect
that there is a more elegant way to do it in Python. Is there? If so: How?
 
P

Paul Rubin

Jan Danielsson said:
I have a list of integers:
q = [ 1, 2, 4, 7, 9 ]
which I would like to convert to a string:
"1,2,4,7,9"

s = ','.join([str(n) for n in q])

Alternatively, just repr(q) gives you '[1, 2, 4, 7, 9]' (with the
brackets and spaces).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem Splitting Text String 2
opposite of __init__.py 1
String splitting with exceptions 7
Opposite of split 16
opposite of zip()? 11
Splitting on '^' ? 10
RE Help splitting CVS data 7
Opposite of repr() (kind of) 3

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top