The format operator
% works on strings for variable substitution purposes. When applied to integers,
% is the modulus operator. But when the first operand is a string,
% is the format operator.
a = 73
b = "%d" % a
73. Basically, it substituted
%d with the value of the variable
a. the format sequence
%d means that the second operand should be formatted as an integer ("d" stands for "decimal").
For more than one format sequence in the string, the second argument has to be a tuple. Each format sequence is matched with an element of the tuple, in order.
The following example uses
%d to format an integer, %g to format a floating-point number, and %s to format a string:
b = "%d is an integer, %g is floating-point and %s is string" % (23, 23.34, "Twenty-Three")
'23 is an integer, 23.34 is floating-point and Twenty-Three is string'
The number of elements in the tuple must match the number of format sequences in the string. The types of elements also must match the format sequences.