title: How to properly add quotes to a string using python?
body: <p>I want to add a set of (double) quotes to a python string if they are missing but the string can also contain quotes.</p>
  
  <p>The purpose of this is to quote all command that are not already quoted because Windows API requires you to quote the entire command line when you execute a process using <a href="http://msdn.microsoft.com/en-us/library/96ayss4b.aspx" rel="nofollow noreferrer">_popen()</a>.</p>
  
  <p>Here are some strings that <em>should</em> be quoted:</p>
  
  <pre><code>&lt;empty string&gt;
  type
  "type" /?
  "type" "/?"
  type "a a" b
  type "" b
  </code></pre>
  
  <p>Here are some that should <strong>not</strong> be quoted:</p>
  
  <pre><code>"type"
  ""type" /?"
  </code></pre>
  
  <p>Please take the time to <strong>test all examples</strong>; it is not too easy to detect if the string needs the quotes or not.</p>