Your code is correct, but I believe there is a preferable way to do this:
>>> my_message = 'Dear {:s}, hello.' >>> my_message.format('jj') 'Dear jj, hello.'
Preferably, due to a possible future replacement of the %
operator with the .format()
method.
As mentioned in Mark Lutz Learning Python. Fourth edition, format
method:
- has several functions not found in the
%
expression, - can make substitution values ββmore explicit,
- trades the operator, perhaps more than the name of the mnemonic method,
- does not support different syntax for single and multiple substitution cases,
and then in the same book:
... there is some risk that Python developers may refuse the %
expression in favor of the format
method in the future. In fact, there is a note in the Python 3.0 manual.
source share