Iteration, Tables, and String Formatting

Description

Often you want to create a nice table to display numbers you have computed using Sage. One easy way to do this is to use string formatting. Below, we create three columns each of width exactly 6 and make a table of squares and cubes.

Sage Cell

Code

for i in range(5):
    print('%6s %6s %6s' % (i, i^2, i^3))

Options

None

Tags

Primary Tags—Programming: The Python language.

Secondary Tags—The Python language: The print statement, Control flow statements.

Related Cells

Counting and Iteration

Attribute

Permalink:

Author: Sage Tutorial v7.6

Date: 07 Jul 2017 15:52

Submitted by: Tom Judson

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License