+ 3
Explain this: print("%s"%1) outputs 1.
How does this work?
1 Answer
+ 3
% is string formating operator (when it is used with a string):
formatString % values
where
formatString - is a string with format specifiers like %s, %d...
values is a tuple that contains values to replace format specifiers in the format string.
"%s"%1
it is equal to
"%s" % (1,)
%s means put value converted to string
%s will be replaced with str(1), which is 1
for example:
v = 2.0
print("values of variable %s is %f"%("v", v))
will output
values of variable v is 2.000000
%s is replaced with string "v" (1st value)
and %f is replaced with floating point value of v (2nd value)