Skip a parameter once, but use more times
I am trying to do this:
commands = {'py': 'python% s',' md ':' markdown "% s"> "% s.html"; gnome-open "% s.html",}
commands ['md']% 'file.md'
But, as you can see, commmands ['md'] uses the parameter 3 times, but the commands ['py'] just use it once. How can I repeat a parameter without changing the last line (so just passing the parameter once?)
Note. The accepted answer, although it works for both older and older versions of Python, is not recommended in newer versions of Python.
Since str.format () is brand new, a lot of Python code still uses the% operator. However, since this old formatting style will eventually be removed from the language, str.format () should usually be used.
For this reason, if you are using Python 2.6 or later, you should use str.format instead of the old % operator:
>>> commands = { ... 'py': 'python {0}', ... 'md': 'markdown "{0}" > "{0}.html"; gnome-open "{0}.html"', ... } >>> commands['md'].format('file.md') 'markdown "file.md" > "file.md.html"; gnome-open "file.md.html"' If you are not using 2.6, you can change the line with the dictionary:
commands = { 'py': 'python %(file)s', 'md': 'markdown "%(file)s" > "%(file)s.html"; gnome-open "%(file)s.html"', } commands['md'] % { 'file': 'file.md' } The syntax% () s works with any of the usual types of% formats and accepts the usual other parameters: http://docs.python.org/library/stdtypes.html#string-formatting-operations p>