Main.StringFormatting History

Hide minor edits - Show changes to markup

February 11, 2010, at 03:04 PM MST by 10.84.44.74 -
Changed lines 14-15 from:

"210 = d * f" % (210, n1, n2, n1 * n2)

to:

"210 = d * f" % (210, n1, n2, n1 * n2)

Changed lines 28-29 from:

print "%3s %6s %6s %8s %13s %23s" % ('i', 'i2', 'i3', 'i5', 'i10', 'i**20')

to:

print "%3s %6s %6s %8s %13s %23s" % ('i', 'i2', 'i3', 'i5', 'i10', 'i**20')

Changed lines 31-32 from:
    print "%3d %6d %6d %8d %13d %23d" % (i, i2, i3, i5, i10, i**20)
to:
    print "%3d %6d %6d %8d %13d %23d" %         (i, i2, i3, i5, i10, i**20)
February 11, 2010, at 03:02 PM MST by 10.84.44.74 -
Added lines 1-31:

(:source lang=python:)

"""String formatting see: http://openbookproject.net/thinkcs/python/english2e/ch07.html """

name = "Asa" age = 40 "I am d years old." % (name, age)

n1 = 4 n2 = 5 "210 = d * f" % (210, n1, n2, n1 * n2)

  1. The syntax for the string formatting operation looks like this:
  2. "<FORMAT>" % (<VALUES>)
  3. Example: a table of powers of the numbers 1-10

print "i\ti2\ti3\ti5\ti10\ti**20" for i in range(1,11) :

    print i, '\t', i2, '\t', i3, '\t', i5, '\t', i10, '\t', i**20
  1. A nicer format:

print "%3s %6s %6s %8s %13s %23s" % ('i', 'i2', 'i3', 'i5', 'i10', 'i**20') for i in range(1,11) :

    print "%3d %6d %6d %8d %13d %23d" % (i, i2, i3, i5, i10, i**20)

(:sourceend:)