title: How to properly escape single and double quotes
body: <p>I have a lxml etree HTMLParser object that I'm trying to build xpaths with to assert xpaths, attributes of the xpath and text of that tag.  I ran into a problem when the text of the tag has either single-quotes(') or double-quotes(") and I've exhausted all my options.</p>
  
  <p>Here's a sample object I created</p>
  
  <pre><code>parser = etree.HTMLParser()
  tree = etree.parse(StringIO(&lt;html&gt;&lt;body&gt;&lt;p align="center"&gt;Here is my 'test' "string"&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;), parser)
  </code></pre>
  
  <p>Here is the snippet of code and then different variations of the variable being read in</p>
  
  <pre><code>   def getXpath(self)
       xpath += 'starts-with(., \'' + self.text + '\') and '
       xpath += ('count(@*)=' + str(attrsCount) if self.exactMatch else "1=1") + ']'
  </code></pre>
  
  <p>self.text is basically the expected text of the tag, in this case: Here is my 'test' "string"</p>
  
  <p>this fails when i try to use the xpath method of the HTMLParser object</p>
  
  <pre><code>tree.xpath(self.getXpath())
  </code></pre>
  
  <p>Reason is because the xpath that it gets is this '/html/body/p[starts-with(.,'Here is my 'test' "string"') and 1=1]'</p>
  
  <p>How can I properly escape the single and double quotes from the self.text variable?  I've tried triple quoting, wrapping self.text in repr(), or doing a re.sub or string.replace escaping ' and " with \' and \"</p>