Skip to content
Home » Advanced String Formatting with the format Method

Advanced String Formatting with the format Method

Spread the love

Today we’ll learn some advanced techniques of formatting strings with the format method.

format method

If you haven’t read my article on formatting strings, feel free to read it first.

The indices and keywords within the curly braces may be followed by a colon and a format string. Let’s have a look:

>>> text = "We sold {0:8d} items for ${1:8.2f} to {customer}.".format(5293, 10653, customer = "Mr. Jones")
>>> print(text)
We sold     5293 items for $10653.00 to Mr. Jones.

Here we have 2 positional arguments and one keyword argument. The integer number is formatted to be 8 characters wide (hence the blanks in the formatted text). The second number is formatted to be 8 characters wide and have a precision of two decimal places, which turns the integer from the parameter list into a float in the formatted text.

If we don’t use the indices in the format fields, we just start with the colon:

>>> text = "We sold {:8d} items for ${:8.2f} to {customer}.".format(5293, 10653, customer = "Mr. Jones")
>>> print(text)
We sold     5293 items for $10653.00 to Mr. Jones.

Flags

We can also use some additional flags in the format fields:

<

the string is left-justified within the available space:

>>> text = "{:<10s}: {:8.2f}".format("price", 3.99)
>>> print(text)
price     :     3.99
>

the string is right-justified within the available space:

>>> text = text = "{:>10s}: {:8.2f}".format("price", 3.99)
>>> print(text)
     price:     3.99
0

if used with a number, the output will be padded with zeros:

>>> number = 125
>>> print("The number is {:06d}.".format(number))
The number is 000125.

Here’s a number with a sign:

>>> number = -125
>>> print("The number is {:06d}.".format(number))
The number is -00125.
+

if used with a number, a sign will be used for both positive and negative numbers:

num1 = 125
num2 = -125

print("It reads: {:+}".format(num1))
print("It reads: {:+}".format(num2))

And here’s the output:

It reads: +125
It reads: -125

Here’s an example with a float:

num1 = 125.548
num2 = -125.548

print("It reads: {:+8.1f}".format(num1))
print("It reads: {:+8.1f}".format(num2))

And here’s the output:

It reads:   +125.5
It reads:   -125.5
-

if used with a number, only a negative sign will be used, but not the positive one:

num1 = 125
num2 = -125

print("It reads: {:-}".format(num1))
print("It reads: {:-}".format(num2))

And here’s the output:

It reads: 125
It reads: -125
(space)

if used with a number, a negative sign will be used on negative numbers and a leading space on positive ones:

num1 = 125
num2 = -125

print("It reads: {: }".format(num1))
print("It reads: {: }".format(num2))

And here’s the output:

It reads:  125
It reads: -125
,

if used with a number, a comma will be used as the thousands separator:

>>> distance = 1789542145551
>>> print("The distance to the star is {:,} km.".format(distance))
The distance to the star is 1,789,542,145,551 km.

It’s used after width but before precision:

>>> amount = 1452254788445.44577885
>>> print("The exact amount was {:30,.3f}.".format(amount))
The exact amount was          1,452,254,788,445.446.
=

if used with a number, it will pad the number with blanks which will be placed after the sign if any:

>>> print("{:=10}".format(24))
        24

>>> print("{:=10}".format(-24))
-       24

If we combine = with 0 or +, we can get other results:

>>> print("{:=010d}".format(-24)) # pad with zeros
-000000024

>>> print("{:=+010d}".format(24)) # also use sign
+000000024
^

the string is left-justified within the available space:

used to center the string within the available space:

>>> print("O {:^20s} O".format("AAA"))
O         AAA          O

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of this article:


Spread the love

Leave a Reply