Looking for an easy way to format variables for display, and mix them with other
text? Add thousands separators (commas in the US)
to a large number, and format decimals the way you want them? You need
“string formatting.”

Python has three ways to format strings. The old string formatting which
originally came from how older languages such as C did formatting.
The newer way introduced in Python 3 that was supposed to be better, but
arguably wasn’t. And the new way introduced in Python 3.6 with
PEP-498 . We’ll cover the
newest way.

This is better; we have right justified numbers! But how does it work?
The :3 that we added isn’t exactly intuitive. Looks like we just
added a random emoji.

Here’s the breakdown: The {} tells the computer we are going to format a
number. Inside we put the variable we want to format, x in this case.
After the variable, we put a : to tell the computer we are about to give it
formatting information.

In this case we give it a 3 to specify a field width of three characters. The
field width value tells the computer to try to fit the number into a field
three characters wide. By default, it will try to right-justify numbers and
left-justify text.

Even better, the program no longer needs to call str() to convert the
number to a string! Leave the string conversions out.

We added a comma after the field width specifier, and now our numbers have
commas. That comma must go after the field width specifier, not before. Commas
are included in calculating the field width. For example, 1,024 has a field
width of 5, not 4.

We can print multiple values, and combine the values with text. Run the code
below.

A format of .2 means to display the number with two digits of precision.
Unfortunately this means if we display the number 123 which has three
significant numbers rather than rounding it we get the number in scientific
notation: 1.2e+02.

A format of .2f (note the f) means to display the number with two digits
after the decimal point. So the number 1 would display as 1.00 and the number
1.5555 would display as 1.56.

The format 10.2f does not mean 10 digits before the decimal and two after.
It means a total field width of 10. So there will be 7 digits before the
decimal, the decimal which counts as one more, and 2 digits after.

Remember! It would be easy to think that %5.2f would mean five digits, a
decimal, followed by two digits. But it does not. It means a total field width
of five, including the decimal and the two digits after. Here’s the output:

defon_draw(self):""" Use this function to draw everything to the screen. """# Start the render. This must happen before any drawing# commands. We do NOT need an stop render command.arcade.start_render()# Calculate minutesminutes=int(self.total_time)//60# Calculate seconds by using a modulus (remainder)seconds=int(self.total_time)%60# Figure out our outputoutput=f"Time: {minutes:02d}:{seconds:02d}"# Output the timer text.arcade.draw_text(output,300,300,arcade.color.BLACK,30)defupdate(self,delta_time):""" All the logic to move, and the game logic goes here. """self.total_time+=delta_time

You can also use it to display the score, or any other statistics you’d like to
show the player.