tags:

views:

90

answers:

4

I have a LIST: ['tag1', 'tag2', 'tag3 tag3', ...]

How do I delete [, ], '. I want to get a string: "tag1, tag2, tag3 tag3, ..."

+3  A: 

One simple way, for your example, would be to take the substring that excludes the first and last character.

myStr = myStr[1:-1]
Kai
+1  A: 
> import re
>>> s = "[test, test2]"
>>> s = re.sub("\[|\]", "", s)
>>> s
'test, test2'
Shawn Simon
haven't tested this one, need to install python at work ;p
Shawn Simon
@Shawn Simon: http://codepad.org
Tomalak
misinterpreted the question anyway
Shawn Simon
A: 

If the '[' and ']' will always be at the front and end of the string, you can use the string strip function.

s = '[tag1, tag2, tag3]'
s.strip('[]')

This should remove the brackets.

Paul
+4  A: 

if you have a list of strings you could do:

>>> lst = ['tag1', 'tag2', 'tag3 tag3']
>>> ', '.join(lst)
'tag1, tag2, tag3 tag3'

Note: you do not remove characters [, ], '. You're concatenating elements of a list into a string. Original list will remain untouched. These characters serve for representing relevant types in python: lists and string, specifically.

SilentGhost
Thank you! In the end, summed up:tags = str(' '.join(args[1:]))[1:-1]
Anry
@Anry: again, representation of string is not the same as content of the string. the output that I posted **doesn't have** any quotes inside it. It is only for you to see that it is a string.
SilentGhost