String manipulation in Python: Quotation marks and apostrophes
<p>So I'm having this issue with python: I'm trying to create a string I can use for a gdal operation using os.system on python. My aim is using this operation for a variable number of inputfiles:</p>

<pre><code>os.system('gdal_calc.py -A {0} -B {1} -C {2} --outfile={4} --calc="(1-A)(1-B)(1-C)" '.format(File1, File2, File3, outputfile))
</code></pre>

<p>So here's how my function looked like:</p>

<pre><code>def Mergefilesforoperation(Inputfiles= None):

    ABC = ["A","B","C","D","E","F","G","H","I","J","K",
    "L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    partstring = []

    for i in range(len(Inputfiles)-1):
        partstring.append(" -" + ABC[i] +  " '" + Inputfiles[i] + "'")


    wholestring = "".join(partstring)  
    outputfile = 'outputfile.tif'
    operationstring = "gdal_calc.py" + wholestring + " --outfile= {0}".format(outputfile) + ' --calc="(1-A)*(1-B)*(1-C)*(1-D)"'

    return wholestring, operationstring
</code></pre>

<p>But when I return the fuction with: </p>

<pre><code>File1 = '/home/File1.tif' File2 = '/home/File2.tif' File3 = '/home/File3.tif'

Mergefilesforoperation(Inputfiles=[File1, File2, File3]) 
</code></pre>

<p>it gives me the following output:</p>

<pre><code>Wholestring = " -A '/home/File1.tif' -B '/home/File2.tif' -C '/home/File3.tif'"

operationstring = 'gdal_calc.py -A \'/home/File1.tif\' -B \'/home/File2.tif\' -C \'/home/File3.tif\' --outfile= outputfile.tif --calc="(1-A)(1-B)(1-C)"')
</code></pre>

<p>how do I get rid of the \ sign after the alphabet in "operationstring"?</p>

<p>For this to work you will have to enclose double quote inside single quote and not other way around.</p>

<p>Though in most of the scenarios both of then should have identical impact but this is one of the corner scenario where it matters Its because of the ways string are represented internally in Python. </p>

<pre><code>&gt;&gt;&gt; os.system('dir "C:\\Program Files"')
 Volume in drive C has no label.
 Volume Serial Number is 26BC-5144

 Directory of C:\Program Files

03-10-2016  12:41    &lt;DIR&gt;          .
03-10-2016  12:41    &lt;DIR&gt;          ..
18-07-2016  12:29    &lt;DIR&gt;          Bonjour


&gt;&gt;&gt; os.system("dir 'C:\\Program Files'")
The filename, directory name, or volume label syntax is incorrect.
1
</code></pre>

<p>Also notice the difference how the strings are represented internally</p>

<pre><code>&gt;&gt;&gt; a
"dir 'C:\\Program Files'"
&gt;&gt;&gt; a.__repr__()
'"dir \'C:\\\\Program Files\'"'
&gt;&gt;&gt; a = 'dir "C:\\Program Files"'
&gt;&gt;&gt; a.__repr__()
'\'dir "C:\\\\Program Files"\''
</code></pre>

python string operating-system