1

I have a List object like this ['tag1', 'tag2', 'tag3 tag3', ...]

How I can skip [, ], ' characters and get a string "tag1, tag2, tag3 tag3, ..."?

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.
3

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

myStr = myStr[1:-1]

Comments

2
> import re
>>> s = "[test, test2]"
>>> s = re.sub("\[|\]", "", s)
>>> s
'test, test2'

1 Comment

misinterpreted the question anyway
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.