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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.