Use str.format(*args, **kwargs
:
"Successfully created the file: {0}.txt".format(iFile)
Example:
In [1]: iFile = "foo"
In [2]: "Successfully created the file: {0}.txt".format(iFile)
Out[2]: 'Successfully created the file: foo.txt'
Edit
Since you seem to have a file, not a file name, you could do this:
In [4]: iFile = open("/tmp/foo.txt", "w")
In [5]: "Successfully created the file: {0}.txt".format(iFile)
Out[5]: "Successfully created the file: <_io.TextIOWrapper name='/tmp/foo.txt' mode='w' encoding='UTF-8'>.txt"
In [6]: "Successfully created the file: {0}.txt".format(iFile.name)
Out[6]: 'Successfully created the file: /tmp/foo.txt.txt'
Note that now the output is foo.txt.txt
with a double extension. If you don't want this because the name of the file already is foo.txt
, you don't should print the additional extension.
Using %
is the old way to format strings. The current Python tutorial explains format
in detail.