orpqK
orpqK

Reputation: 2795

To strip a certain character out in Python

I have a string str="[0,1,12]"

What is the most efficent way to strip out the "[" ,"]", and the "," only to leave the numbers as a list behind? I tried .rstrip(), but it only takes the characters at end of the string.

Upvotes: 1

Views: 120

Answers (1)

Blender
Blender

Reputation: 298582

Evaluate the string into a list:

from ast import literal_eval

numbers = literal_eval(your_string)

Upvotes: 6

Related Questions