All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Formatting output with f-strings

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

Exam tasks often ask for output in columns with set widths, alignment and decimal places. f-strings do this.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

f-strings (spec 8.2.2)

Put f before the quotes and expressions inside braces. After a colon, a format specifier controls the layout: {value:align width .precision type}.

name = "Ana"
score = 7.456
print(f"{name} scored {score:.2f}")
print(f"{name:<8}|{name:>8}|{name:^8}|")
print(f"{42:5d}|{42:<5d}|{3.14159:8.3f}|")
print(f"{5:+d} {-5:+d}")

Output:

Ana scored 7.46
Ana     |     Ana|  Ana   |
   42|42   |   3.142|
+5 -5

The parts of a format specifier

PartSymbolsMeaning
Alignment< > ^left (default for text), right (default for numbers), centre
Sign+ - space+ always shows a sign; - only for negatives (default); a space leaves room for a sign
Widtha whole numberminimum number of characters; padded with spaces
Precision.ndigits after the decimal point for f
Types d fstring, base-10 integer, fixed-point real

Tables of output

Use the same widths for headings and data so the columns line up:

data = [["Pen", 3, 0.5], ["Notebook", 12, 2.25]]
print(f"{'Item':<10}{'Qty':^5}{'Price':>8}")
for item in data:
    print(f"{item[0]:<10}{item[1]:^5d}{item[2]:>8.2f}")

Output:

Item       Qty    Price
Pen         3      0.50
Notebook   12      2.25

The sample paper awarded marks for headings, centre-aligned text and integers and right-aligned real numbers.

format() and str()

"{:>6.1f}".format(x) uses the same specifiers. Joining with str() and + also works, but you then control spacing yourself.

Worked example

Display the price 4.5 right-aligned in a width of 8 with 2 decimal places, preceded by the label 'Total:'.

  1. The specifier is :>8.2f
  2. f"Total:{4.5:>8.2f}"
  3. Output: Total:····4.50

Answer: Total:····4.50 (each · is a space)

Key idea: In {value:spec}, < > ^ set alignment, a number sets the width, .n sets decimal places and s, d, f set the type; use matching widths for headings and rows.

Check yourself

  1. What is displayed when this code runs? ```python print(f"{2/3:.3f}") ```
    Show answer

    0.667. 2/3 = 0.6666..., shown to 3 decimal places: 0.667.

  2. How many characters long is the string produced by f"{'cat':>7}"?
    Show answer

    7. The width of 7 pads 'cat' with 4 spaces on the left, making 7 characters.

  3. What is displayed when this code runs? ```python print(f"[{'ab':6}]") ```
    • ``` [ab ] ```
    • ``` [ ab] ```
    • ``` [ ab ] ```
    • ``` [ab] ```
    Show answer

    ``` [ ab ] ```. ^ centres the text in a width of 6: two spaces each side: ``` [ ab ] ```

  4. What is displayed when this code runs? ```python print(f"{7:+d}|{-7:+d}") ```
    • 7|-7
    • +7|-7
    • +7|+7
    • +7|7
    Show answer

    +7|-7. The + sign option always shows the sign: +7|-7.

  5. What is displayed when this code runs? ```python print(f"{3.0:.0f}|{17:04d}") ```
    • 3.0|17
    • 3|17
    • 3.|0017
    • 3|0017
    Show answer

    3|0017. .0f shows no decimal places, and 04d pads the integer with zeros to width 4: 3|0017.

  6. This line should display a mark such as 67.5 right-aligned in 6 characters with 1 decimal place, between bars. Which line completes it? ```python mark = 67.5 __________ ```
    • `print(f"|{mark:<6.1f}|")`
    • `print(f"|{mark:>6.1f}|")`
    • `print(f"|{mark:>1.6f}|")`
    • `print(f"|{mark:6d}|")`
    Show answer

    `print(f"|{mark:>6.1f}|")`. > right-aligns, 6 is the width and .1f gives 1 decimal place.

  7. Without an alignment symbol, how are numbers and strings aligned in a wider field?
    • Numbers left, strings right
    • Centred, whatever the type
    • Left, whatever the type
    • Numbers right, strings left
    Show answer

    Numbers right, strings left. The defaults are right alignment for numbers and left alignment for text.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.