By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,712 Members | 763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,712 IT Pros & Developers. It's quick & easy.

How to convert this list to string?

P: n/a
Hi all

I have a list like:
>>list
[1, 2, 3]
>>list[1:]
[2, 3]

I want to get a string "2 3"
>>str(list[1:])
'[2, 3]'

How can I do that ?

thanks

Oct 18 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 18 Oct 2006 00:20:50 -0700, Jia Lu <Ro*****@gmail.comwrote:
I want to get a string "2 3"
>str(list[1:])
'[2, 3]'

How can I do that ?
' '.join(str(i) for i in list[1:])

-- Theerasak
Oct 18 '06 #2

P: n/a
Jia Lu wrote:
Hi all

I have a list like:
>>>list
[1, 2, 3]
>>>list[1:]
[2, 3]

I want to get a string "2 3"
>>>str(list[1:])
'[2, 3]'

How can I do that ?
" ".join(str(x) for x in list)
-Travis

Oct 18 '06 #3

P: n/a
Jia Lu wrote:
Hi all

I have a list like:
>>>list
[1, 2, 3]
>>>list[1:]
[2, 3]

I want to get a string "2 3"
>>>str(list[1:])
'[2, 3]'

How can I do that ?
http://effbot.org/zone/python-list.htm#printing

</F>

Oct 18 '06 #4

P: n/a
Jia Lu wrote:
Hi all

I have a list like:
>>>list
[1, 2, 3]
>>>list[1:]
[2, 3]

I want to get a string "2 3"
>>>str(list[1:])
'[2, 3]'

How can I do that ?

thanks
Just to be different from the other suggestions...
>>a = [1, 2, 3]
str(a[1:]).strip('[]').replace(',', '')
'2 3'

By the way. It's a good idea to try not to use 'list' or other built-in names
for your own objects. Best to start with good habits so that you avoid odd hard
to find bugs later.

Cheers,
Ron
Oct 18 '06 #5

P: n/a
Thank you very much. I memoed all you views.

:)

Oct 18 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.