← All lessons

Formatting and alignment

Sign in · Sign up

Lesson 4.7

An f-string puts values right inside the text, and after a colon you can set the width and the side to align to:

Form What it does
f"Hello, {name}" inserts the value
f"{name:>10}" width 10, right-aligned
f"{name:<10}" width 10, left-aligned
f"{name:^10}" width 10, centred

The letter f before the quotes is required: without it the curly braces stay as text.

The name name, the number score and the width width are set by the check. Print, in order:

  1. Name: <name> — with a label;
  2. the name right-aligned in a field of width, between two |;
  3. the number left-aligned in a field of width, between two |.
Hint

Alignment is set in the f-string after the colon: <, >, ^.

{name:>10} pads on the left, {age:<5} pads on the right, ^ centres. The width is the total number of characters, including the value itself.