Use string in subprocess
Question: I've written Python code to compute an IP programmatically, that I then want
to use in an external connection program.

I don't know how to pass it to the subprocess:

    
    
    import subprocess
    from subprocess import call
    
    some_ip = "192.0.2.0"  # Actually the result of some computation,
                           # so I can't just paste it into the call below.
    
    subprocess.call("given.exe -connect  host (some_ip)::5631 -Password")  
    

I've read what I could and found similar questions but I truly cannot
understand this step, to use the value of `some_ip` in the subprocess. If
someone could explain this to me it would be greatly appreciated.


Answer: If you don't use it with `shell=True` (and I don't recommend `shell=True`
unless you really know what you're doing, as shell mode can have security
implications) `subprocess.call` takes the command as an sequence (e.g. a
`list`) of its components: First the executable name, then the arguments you
want to pass to it. All of those should be strings, but whether they are
string literals, variables holding a string or function calls returning a
string doesn't matter.

Thus, the following should work:

    
    
    import subprocess
    
    some_ip = "192.0.2.0"  # Actually the result of some computation.
    
    subprocess.call(
        ["given.exe", "-connect",  "host", "{}::5631".format(some_ip), "-Password"])
    

  * I'm using [`str`'s `format` method](https://docs.python.org/3.4/library/stdtypes.html#str.format) to replace the `{}` placeholder in `"{}::5631"` with the string in `some_ip`.
  * If you invoke it as `subprocess.call(...)`, then
    
        import subprocess
    

is sufficient and

    
        from subprocess import call
    

is unnecessary. The latter would be needed if you want to invoke the function
as just `call(...)`. In that case the former import would be unneeded.

