I have a LIST: ['tag1', 'tag2', 'tag3 tag3', ...]
How do I delete [
, ]
, '
. I want to get a string: "tag1, tag2, tag3 tag3, ..."
I have a LIST: ['tag1', 'tag2', 'tag3 tag3', ...]
How do I delete [
, ]
, '
. I want to get a string: "tag1, tag2, tag3 tag3, ..."
One simple way, for your example, would be to take the substring that excludes the first and last character.
myStr = myStr[1:-1]
> import re
>>> s = "[test, test2]"
>>> s = re.sub("\[|\]", "", s)
>>> s
'test, test2'
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.
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.