Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the context you are asking about the <code>%</code> is used to help format output, and has nothing to do with any mod operation.</p> <p>For instance, <code>%3d</code>, <code>%.2f</code> 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.</p> <p>See<code>String Formatting Operations</code> in <a href="http://docs.python.org/library/stdtypes.html#string-formatting-operations" rel="nofollow">http://docs.python.org/library/stdtypes.html#string-formatting-operations</a> for more information and details about the various formatting types and options.</p> <p>You may come across something like this:</p> <pre><code>number_of_Widgets = 5 cost = 66.8788 print 'Total cost for %d widgets is $ %5.2f.' % (number_of_Widgets, cost) </code></pre> <p>yields:</p> <pre><code>Total cost for 5 widgets is $ 66.88. </code></pre> <p>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.</p> <p>Here is a simple example as to why the format strings can come in handy for formatting your output:</p> <p>Here we don't reserve and additional space for the number and you can see it shifting the output when it reaches 10.</p> <pre><code>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 </code></pre> <p>Here we format the number with two spaces, and get a leading blank space for single digit numbers.</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload