python – Multi line string with arguments. How to declare?

python – Multi line string with arguments. How to declare?

You could use the str.format() function, that allows named arguments, so:

line {0}
line {1}
line {2}.format(1,2,3)

You could of course extend this using Pythons *args syntax to allow you to pass in a tuple or list:

args = (1,2,3)
line {0}
line {1}
line {2}.format(*args)

If you can intelligently name your arguments, the most robust solution (though the most typing-intensive one) would be to use Pythons **kwargs syntax to pass in a dictionary:

args = {arg1:1, arg2:2, arg3:3}
line {arg1}
line {arg2}
line {arg3}.format(**args)

For more information on the str.format() mini-language, go here.

You could abuse the line continuation properties of the parenthesis ( and the comma ,.

cmd = line %d
      line %d
      line %d % (
      1,
      2,
      3)

python – Multi line string with arguments. How to declare?

The easiest way might be to use literal string interpolation (available from Python 3.6 onwards and assuming all the arguments are in scope).

cmd = fline {1}
      line {2}
      line {3}

Leave a Reply

Your email address will not be published. Required fields are marked *