[Tutor] Defining a "format string"
Noah Hall
enalicho at gmail.com
Sun Jun 26 13:18:31 CEST 2011
On Sun, Jun 26, 2011 at 12:05 PM, Lisi <lisi.reisz at gmail.com> wrote:
> In the following excerpt from a program in the book I am following:
>
> print "If I add %d, %d, and %d I get %d." % (
> my_age, my_height, my_weight, my_age + my_height + my_weight)
>
> is
>
> % (
> my_age, my_height, my_weight, my_age + my_height + my_weight)
>
> the/a format string?
No.
(my_age, my_height, my_weight, my_age + my_height + my_weight)
are the values (variables in this case, but it could be direct values too)
"If I add %d, %d, and %d I get %d."
is the formatted string (what you want to see outputted as a string)
So, say we had -
my_age = 65
my_height = 185
my_weight = 11
Then what'd happen is -
print "If I add %d, %d, and %d I get %d." % (65, 185, 11, 65 + 185 + 11)
Notice how the values are actual values? The other text part is the
format for the values to take inside a string, so you get -
print "If I add 65, 185 and 11 I get 261"
HTH
More information about the Tutor
mailing list