I've got something like this:
a = '2(3.4)'
b = '12(3.5)'
I only want the value inside the brackets. I used regex, and it worked, but my teacher won't allow it. How can I do this?
>>> a = '2(3.4)'
>>> a[a.index("(") + 1:a.rindex(")")]
'3.4'
>>> a, b = '2(3.4)', '12(3.5)'
>>> def extract(string, start='(', stop=')'):
return string[string.index(start)+1:string.index(stop)]
>>> extract(a), extract(b)
('3.4', '3.5')
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With