0

I am reading thru DiveIntoPython docs and it gives some examples
Example 3.23. Formatting Numbers
>>> print "Today's stock price: %f" % 50.4625
50.462500

The 1st % designates Formatting, what does the 2nd % do?
Thanks!

5
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by bvdet
0

that just makes sure the program knows where to look to find the variables. So if more than one thing was in the formatted string you would use a tuple.

0

That is:
s = "---> Increased"
print "Today's stock price: %f %s" % (50.4625, s)

You got it ;)

0

that just makes sure the program knows where to look to find the variables. So if more than one thing was in the formatted string you would use a tuple.

It's required to enclose the variable in parentheses in this case:

>>> a = 5
>>> b = 10
>>> print "%0.4f" % a*b
5.00005.00005.00005.00005.00005.00005.00005.00005.00005.0000
>>> print "%0.4f" % b/a
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
TypeError: unsupported operand type(s) for /: 'str' and 'int'
>>> print "%0.4f" % (a*b)
50.0000
>>>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.