1 | initial version |
This 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 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.
The Python documentation for this is quite technical; see this site for some good examples.