Library contents

Python str.format

Insert a student or parcel identifier into a reusable display template.

A simple example

str.format

  1. Start with Text A, template [value]
  2. Do this Insert text between brackets
  3. Result "[A]"
Formatting keeps the brackets and fills the single space reserved for a value.

Syntax

Python syntax
"ID [{}]".format(value)
Run these examples in Python

Save one example at a time as lesson.py. Use Python 3.9 or later.

python3 lesson.py

Print a student badge

A printer receives badge or ticket data in separate fields. Use the display format required by the desk so names, numbers and separators occupy their intended places on the finished label.

From the programvalue = "Ada"
ResultID [Ada]
lesson.py
value = "Ada"
print("ID [{}]".format(value))
Output
ID [Ada]

The brackets come from the template.

Advertisements

Print a parcel badge

A parcel badge combines data supplied by the shipping system into one printable line. Format those fields in the order the warehouse worker needs to read them during a handoff.

From the programvalue = "PK-17"
ResultID [PK-17]
lesson.py
value = "PK-17"
print("ID [{}]".format(value))
Output
ID [PK-17]

The identifier is inserted unchanged.

Print the first entry tag

A coat-check desk numbers the first ticket one. The ticket printer includes the number as ordinary decimal text after a prefix. What does the completed label say?

GivenPrefix “Ticket ” + 1
Worked resultTicket 1

What to watch for

Treat the template as application code and pass user-provided values as arguments.

Documentation for str.format