Beautiful soup alone can't get a substring. You can use regex along with it.
from bs4 import BeautifulSoup
import re
html = """<p>
A
<span>die</span>
is thrown \(x = {-b \pm
<span>\sqrt</span>
{b^2-4ac} \over 2a}\) twice. What is the probability of getting a sum 7 from
both the throws?
</p>"""
soup = BeautifulSoup(html, 'html.parser')
print re.findall(r'\\\(.*?\)', soup.text, re.DOTALL)
Output:
[u'\\(x = {-b \\pm \n \\sqrt\n {b^2-4ac} \\over 2a}\\)']
Regex:
\\\(.*?\) - Get substring from ( to ).
If you want to strip the newlines and whitespaces, you can do it like so:
res = re.findall(r'\\\(.*?\)', soup.text, re.DOTALL)[0]
print ' '.join(res.split())
Output:
\(x = {-b \pm \sqrt {b^2-4ac} \over 2a}\)
HTML wrappers around the string:
print BeautifulSoup(' '.join(res.split()))
Output:
<html><body><p>\(x = {-b \pm \sqrt {b^2-4ac} \over 2a}\)</p></body></html>