If you're sure you have that string, slice both characters and add the ]
back on!
source_string = "[2,3,1,1,]"
if source_string.endswith(",]"):
source_string = source_string[:-2] + "]"
However, often lists stored as strings are not very useful - you may really want to convert the whole thing to a collection of numbers (perhaps manually removing the "[]"
and splitting by ,
, or using ast.literal_eval()
), potentially converting it back to a string to display later
>>> source_string = "[2,3,1,1,]"
>>> import ast
>>> my_list = ast.literal_eval(source_string)
>>> my_list
[2, 3, 1, 1]
>>> str(my_list)
'[2, 3, 1, 1]'