akrisanov
akrisanov

Reputation: 3214

Getting a string from Python list?

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

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

Upvotes: 1

Views: 7193

Answers (4)

SilentGhost
SilentGhost

Reputation: 319601

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.

Upvotes: 5

Paul
Paul

Reputation: 1

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.

Upvotes: 0

Shawn
Shawn

Reputation: 19803

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

Upvotes: 2

Kai
Kai

Reputation: 9552

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

myStr = myStr[1:-1]

Upvotes: 3

Related Questions