Oberon Meier
Oberon Meier

Reputation: 27

How do I start reading from a certain character in a string?

I have a list of strings that look something like this:

"['id', 'thing: 1\nother: 2\n']"
"['notid', 'thing: 1\nother: 2\n']"

I would now like to read the value of 'other' out of each of them. I did this by counting the number at a certain position but since the position of such varies I wondererd if I could read from a certain character like a comma and say: read x_position character from comma. How would I do that?

Upvotes: 0

Views: 725

Answers (1)

DYZ
DYZ

Reputation: 57033

Assuming that "other: " is always present in your strings, you can use it as a separator and split by it:

s = 'thing: 1\nother: 2'
_,number = s.split('other: ')
number
#'2'

(Use int(number) to convert the number-like string to an actual number.) If you are not sure if "other: " is present, enclose the above code in try-except statement.

Upvotes: 1

Related Questions