title: String format python
body: <p>I'm trying to clean up a bad solution. I have a utils file, which contains some generic methods and a commands file, where I store a list of commands to use within the program. However, when trying to clean up files by using string format, I'm starting to run into problems.</p>
  
  <pre><code># utils.py
  import os
  
  bin = '/usr/bin'
  tool = '{0}/tool'.format(bin)
  command = '{0} -dir %s -o geniso'.format(tool)
  
  def exec_cmd(cmd):
      os.system(cmd)
  
  # dowork.py
  from utils import *
  exec_cmd(command % '/var/tmp')
  </code></pre>
  
  <p>In the example you can see, I'm trying to clean up the commands/utils by concatenating variables to reduce the amount of characters in the variable and this works fine. The problem is when I'm trying to then extend that format, with another variable, for example a directory name (which gets passed from a different class), I'm getting confused on how to efficiently do this formatting. I'm also hitting the follow exception:</p>
  
  <pre><code>Traceback (most recent call last):
    File "some_script.py", line 12, in &lt;module&gt;
      import commands as cmd
    File "/var/tmp/commands.py", line 32, in &lt;module&gt;
      SOME_CMD = '{0} -p %s | tail -n +4 | awk \'{print \$4}\''.format(SOMETOOL)
  KeyError: 'print \\$4'
  </code></pre>
  
  <p>How can I clean up this absolute mess, into a way that I can nicely format my commands file but also leave room for further extension when using the commands within a script.</p>