"""String formatting
see: http://openbookproject.net/thinkcs/python/english2e/ch07.html
"""
name = "Asa"
age = 40
"I am %s and I am %d years old." % (name, age)
n1 = 4
n2 = 5
"2**10 = %d and %d * %d = %f" % \
(2**10, n1, n2, n1 * n2)
# The syntax for the string formatting operation looks like this:
# "<FORMAT>" % (<VALUES>)
# Example: a table of powers of the numbers 1-10
print "i\ti**2\ti**3\ti**5\ti**10\ti**20"
for i in range(1,11) :
print i, '\t', i**2, '\t', i**3, '\t', i**5, '\t', i**10, '\t', i**20
# A nicer format:
print "%3s %6s %6s %8s %13s %23s" % \
('i', 'i**2', 'i**3', 'i**5', 'i**10', 'i**20')
for i in range(1,11) :
print "%3d %6d %6d %8d %13d %23d" % \
(i, i**2, i**3, i**5, i**10, i**20)
