title: What is the best way to escape Python strings in PHP?
body: <p>I have a PHP application which needs to output a python script, more specifically a bunch of variable assignment statements, eg.</p>
  
  <pre><code>subject_prefix = 'This String From User Input'
  msg_footer = """This one too."""
  </code></pre>
  
  <p>The contents of subject_prefix et al need to be written to take user input; as such, I need to escape the contents of the strings. Writing something like the following isn't going to cut it; we're stuffed as soon as someone uses a quote or newline or anything else that I'm not aware of that could be hazardous:</p>
  
  <pre><code>echo "subject_prefix = '".$subject_prefix."'\n";
  </code></pre>
  
  <p>So. Any ideas?</p>
  
  <p>(Rewriting the app in Python isn't possible due to time constraints. :P )</p>
  
  <p><strong>Edit, years later:</strong></p>
  
  <p>This was for integration between a web-app (written in PHP) and Mailman (written in Python). I couldn't modify the install of the latter, so I needed to come up with a way to talk in its language to manage its configuration.</p>
  
  <p>This was also a <em>really</em> bad idea.</p>