ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 10 Apr 2017 19:56:27 -0500Sage % symbolhttp://ask.sagemath.org/question/37243/sage-symbol/I am a beginner to Sage. During learning Sage I found a code snippet here
for i in range(5):
print('%6s %6s %6s' % (i, i^2, i^3))
Can anybody explain why `%()` is used here ?Sun, 09 Apr 2017 23:17:36 -0500http://ask.sagemath.org/question/37243/sage-symbol/Answer by tmonteil for <p>I am a beginner to Sage. During learning Sage I found a code snippet here</p>
<pre><code>for i in range(5):
print('%6s %6s %6s' % (i, i^2, i^3))
</code></pre>
<p>Can anybody explain why <code>%()</code> is used here ?</p>
http://ask.sagemath.org/question/37243/sage-symbol/?answer=37249#post-id-37249Note that the `%` is kind of being deprecated in favor of the `format` method, your snippet will be replaced as follows:
sage: for i in range(5):
....: print('{:>6} {:>6} {:>6}'.format(i, i^2, i^3))
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
Mon, 10 Apr 2017 08:45:37 -0500http://ask.sagemath.org/question/37243/sage-symbol/?answer=37249#post-id-37249Comment by kcrisman for <p>Note that the <code>%</code> is kind of being deprecated in favor of the <code>format</code> method, your snippet will be replaced as follows:</p>
<pre><code>sage: for i in range(5):
....: print('{:>6} {:>6} {:>6}'.format(i, i^2, i^3))
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
</code></pre>
http://ask.sagemath.org/question/37243/sage-symbol/?comment=37250#post-id-37250True, though apparently not *really* deprecated, as it turns out. For short things the "old syntax" probably still suffices, though I find the new syntax nicer for more flexibility.Mon, 10 Apr 2017 11:14:40 -0500http://ask.sagemath.org/question/37243/sage-symbol/?comment=37250#post-id-37250Answer by kcrisman for <p>I am a beginner to Sage. During learning Sage I found a code snippet here</p>
<pre><code>for i in range(5):
print('%6s %6s %6s' % (i, i^2, i^3))
</code></pre>
<p>Can anybody explain why <code>%()</code> is used here ?</p>
http://ask.sagemath.org/question/37243/sage-symbol/?answer=37247#post-id-37247This is a string formatting symbol. Suppose you have several items `a,b,c` in Sage (or Python more generally) you wish to print, but you want them to be printed in the midst of a longer statement.
a, b, c, = 1, 2, 3
Then to do so, rather than something error-prone like this
print('My first number is ' + str(a) + ' then I have ' + str(b) + ' and also ' + str(c))
or several variants thereon, it is easier and more adaptable to do
print('My first number is %s then I have %s and also %s' %(a, b, c))
This says that you replace each `%s` with the next item in the tuple `(a,b,c)`. If now you realized they were in the wrong order, it's very easy to fix that:
print('My first number is %s then I have %s and also %s' %(c, b, a))
Note that `%s` is for string formatting; there are other ways to format numbers as well. The one in your example is [padding](https://pyformat.info/#string_pad_align) with extra spaces.
print('My first number is %6s then I have %6s and also %6s' %(a, b, c))
See [this link for an active example](http://sagecell.sagemath.org/?z=eJxL1FFI0lFI1lGwVTDUUTDSUTDm5Sooyswr0VD3rVRIyywqLlHIK81NSi1SyCxWUFfQViguKdJI1AQy1BVKMlLzFDwVMhLLUuFSSRCpxLwUhcSc4ny4eLKmJj6TVc2KUYwD8eFmADnqCqoaiRC3ggwCAFxGM2o=&lang=sage).
The Python documentation for this is quite technical; [see this site](https://pyformat.info/#simple) for some good examples.Mon, 10 Apr 2017 07:54:20 -0500http://ask.sagemath.org/question/37243/sage-symbol/?answer=37247#post-id-37247Comment by kcrisman for <p>This is a string formatting symbol. Suppose you have several items <code>a,b,c</code> in Sage (or Python more generally) you wish to print, but you want them to be printed in the midst of a longer statement.</p>
<pre><code>a, b, c, = 1, 2, 3
</code></pre>
<p>Then to do so, rather than something error-prone like this</p>
<pre><code>print('My first number is ' + str(a) + ' then I have ' + str(b) + ' and also ' + str(c))
</code></pre>
<p>or several variants thereon, it is easier and more adaptable to do</p>
<pre><code>print('My first number is %s then I have %s and also %s' %(a, b, c))
</code></pre>
<p>This says that you replace each <code>%s</code> with the next item in the tuple <code>(a,b,c)</code>. If now you realized they were in the wrong order, it's very easy to fix that:</p>
<pre><code>print('My first number is %s then I have %s and also %s' %(c, b, a))
</code></pre>
<p>Note that <code>%s</code> is for string formatting; there are other ways to format numbers as well. The one in your example is <a href="https://pyformat.info/#string_pad_align">padding</a> with extra spaces.</p>
<pre><code>print('My first number is %6s then I have %6s and also %6s' %(a, b, c))
</code></pre>
<p>See <a href="http://sagecell.sagemath.org/?z=eJxL1FFI0lFI1lGwVTDUUTDSUTDm5Sooyswr0VD3rVRIyywqLlHIK81NSi1SyCxWUFfQViguKdJI1AQy1BVKMlLzFDwVMhLLUuFSSRCpxLwUhcSc4ny4eLKmJj6TVc2KUYwD8eFmADnqCqoaiRC3ggwCAFxGM2o=&lang=sage">this link for an active example</a>.</p>
<p>The Python documentation for this is quite technical; <a href="https://pyformat.info/#simple">see this site</a> for some good examples.</p>
http://ask.sagemath.org/question/37243/sage-symbol/?comment=37256#post-id-37256I'm not sure exactly what you want me to clarify, but your comment will hopefully achieve that purpose to future readers of this post. Yes, `%` is also an operator, but I was only referring to the string formatting purpose.Mon, 10 Apr 2017 19:56:27 -0500http://ask.sagemath.org/question/37243/sage-symbol/?comment=37256#post-id-37256Comment by lalthomas for <p>This is a string formatting symbol. Suppose you have several items <code>a,b,c</code> in Sage (or Python more generally) you wish to print, but you want them to be printed in the midst of a longer statement.</p>
<pre><code>a, b, c, = 1, 2, 3
</code></pre>
<p>Then to do so, rather than something error-prone like this</p>
<pre><code>print('My first number is ' + str(a) + ' then I have ' + str(b) + ' and also ' + str(c))
</code></pre>
<p>or several variants thereon, it is easier and more adaptable to do</p>
<pre><code>print('My first number is %s then I have %s and also %s' %(a, b, c))
</code></pre>
<p>This says that you replace each <code>%s</code> with the next item in the tuple <code>(a,b,c)</code>. If now you realized they were in the wrong order, it's very easy to fix that:</p>
<pre><code>print('My first number is %s then I have %s and also %s' %(c, b, a))
</code></pre>
<p>Note that <code>%s</code> is for string formatting; there are other ways to format numbers as well. The one in your example is <a href="https://pyformat.info/#string_pad_align">padding</a> with extra spaces.</p>
<pre><code>print('My first number is %6s then I have %6s and also %6s' %(a, b, c))
</code></pre>
<p>See <a href="http://sagecell.sagemath.org/?z=eJxL1FFI0lFI1lGwVTDUUTDSUTDm5Sooyswr0VD3rVRIyywqLlHIK81NSi1SyCxWUFfQViguKdJI1AQy1BVKMlLzFDwVMhLLUuFSSRCpxLwUhcSc4ny4eLKmJj6TVc2KUYwD8eFmADnqCqoaiRC3ggwCAFxGM2o=&lang=sage">this link for an active example</a>.</p>
<p>The Python documentation for this is quite technical; <a href="https://pyformat.info/#simple">see this site</a> for some good examples.</p>
http://ask.sagemath.org/question/37243/sage-symbol/?comment=37251#post-id-37251Thank you for the answer. I found some documentation about `%` [here](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting). It basically says that `%` is a operator. Similar to operator overloading in C++, it is a modulus operator in Sage too. Can you update your answer to reflect the similar view as of the documentation. It is more clearer.Mon, 10 Apr 2017 12:30:36 -0500http://ask.sagemath.org/question/37243/sage-symbol/?comment=37251#post-id-37251