CHAPTER 21

image

Formatting

Here is a quick table for reference when doing text formatting. For a detailed explanation of how text formatting works, keep reading.

Taba

Decimal Numbers

Try running the following program, which prints out several random numbers.

import randomfor i in range(10):    x = random.randrange(20)    print(x)

The output is left justified and numbers look terrible:

161320103181145

We can use string formatting to make the list of numbers look better by right justifying them. The first step is to use the format command on the string. ...

Get Program Arcade Games: With Python and Pygame, Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.