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.
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 -5The parts of a format specifier
| Part | Symbols | Meaning |
|---|---|---|
| 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 |
| Width | a whole number | minimum number of characters; padded with spaces |
| Precision | .n | digits after the decimal point for f |
| Type | s d f | string, 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.25The 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:'.
- The specifier is :>8.2f
- f"Total:{4.5:>8.2f}"
- Output: Total:····4.50
Answer: Total:····4.50 (each · is a space)
Check yourself
- 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.
- 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.
- 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 ] ```
- 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.
- 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.
- 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.
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.