Reputation: 84
I have searched the forums for a little bit now and I am still very confused. If someone could direct me to an article that explains this more in depth I would greatly appreciate it. I am taking an intro to python course and I found the solution to my problem, but I want to actually know how\why it works. The assignment is to return the batting average for a player, and display it as the MLB would display. So for example 4hits in 8 at bats would be .500. Here is the code I found that works.
from __future__ import division, print_function
def print_batting_average(at_bats, hits):
average_str = "%.3f" % (hits / at_bats)
if average_str.startswith("0"):
average_str = average_str[1:]
print("Batting average is", average_str)
print_batting_average(4, 2)
I know the % is modulus and gives the remainder of something. I just don't understand how that fits into other parts of code. For example the "%.3f". I have also seen the % used in different ways, and I just wanted to find a good explanation. Hopefully I don't get flamed for positing this. Thanks for any help.
Upvotes: 2
Views: 8711
Reputation: 95252
% in an arithmetic expression means modulus.
% inside a literal string like "%.3f" is just a normal character; it has nothing to do with modulus.
But strings with % signs inside them are special when you use them in conjunction with the % operator. In this case, the value on the left is a string, so the modulus meaning doesn't apply; instead, you get formatting. The %-sequences inside the string are placeholders that get replaced by the values in the tuple, formatted according to what comes after each '%' in the string. For i nstance, "%.3f" means "a floating point value with three digits after the decimal point.".
See http://docs.python.org/library/stdtypes.html#string-formatting for details on the format specifications; it's approximately the same as the printf
family of functions in C.
Upvotes: 3
Reputation: 143022
In the context you are asking about the %
is used to help format output, and has nothing to do with any mod operation.
For instance, %3d
, %.2f
are all different ways to format output. The first example means you want to display an integer and reserve 3 spaces for it. The 2nd one means you want to display a float type value with 2 numbers after the decimal point.
SeeString Formatting Operations
in
http://docs.python.org/library/stdtypes.html#string-formatting-operations for more information and details about the various formatting types and options.
You may come across something like this:
number_of_Widgets = 5
cost = 66.8788
print 'Total cost for %d widgets is $ %5.2f.' % (number_of_Widgets, cost)
yields:
Total cost for 5 widgets is $ 66.88.
The part enclosed in ' ' uses formatting instructions as place holders for the actual variable values supplied in toward the end of the line. The values are preceded by the % and then the () contain the variables that supply the values.
Here is a simple example as to why the format strings can come in handy for formatting your output:
Here we don't reserve and additional space for the number and you can see it shifting the output when it reaches 10.
In [9]: for i in xrange(5,15):
...: print '%d is the number' % i
...:
5 is the number
6 is the number
7 is the number
8 is the number
9 is the number
10 is the number
11 is the number
12 is the number
13 is the number
14 is the number
Here we format the number with two spaces, and get a leading blank space for single digit numbers.
In [10]: for i in xrange(5,15):
....: print '%2d is the number' % i
....:
5 is the number
6 is the number
7 is the number
8 is the number
9 is the number
10 is the number
11 is the number
12 is the number
13 is the number
14 is the number
Upvotes: 4
Reputation: 500357
In:
"%.3f" % (hits / at_bats)
the percent sign is used in two different ways, neither of which is related to the modulus operator.
Specifically:
%
is the string formatting operator;%
is used in conjunction with the first, and defines the actual format used.Upvotes: 1
Reputation: 7243
The %
used to format strings is not the same as the modulus operator. Notice how it's inside quotes.
%
for string formatting is a standard and convention in many programming languages - http://en.wikipedia.org/wiki/Printf_format_string
Upvotes: 0
Reputation: 13289
Check this out http://docs.python.org/release/2.6/library/string.html
“Format specifications” are used within replacement fields contained within a format string to define how individual values are presented (see Format String Syntax.) They can also be passed directly to the builtin format() function. Each formattable type may define how the format specification is to be interpreted.
The modulus function in Python is just mod(p,q)
Upvotes: 0