Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

A developer wants to create a string that contains the current year and month in the format 'YYYY-MM'. The year and month are stored in integer variables year and month. Which expression would produce the desired result?

⚠ Common exam trap

The PCAP exam often tests the distinction between string concatenation with `+` (which requires both operands to be strings) and f-string formatting, and the trap here is that candidates may forget to zero-pad the month, choosing Option C because it 'works' syntactically but fails the format requirement.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

f"{year}-{month:02d}"

Uses an f-string with the format specifier `:02d` to zero-pad the month to two digits, ensuring that months 1-9 appear as '01', '02', etc. This produces the exact 'YYYY-MM' format from integer variables without manual conversion or padding.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    f"{year}-{month:02d}"

    Why this is correct

    The f-string `f"{year}-{month:02d}"` evaluates both expressions inline and applies a format specification to the month placeholder. The `02d` specifier instructs Python to format the integer month as a decimal with a minimum width of two characters, padding with a leading zero when needed. This produces the desired ISO-like format, e.g., `2024-03` for March.

  • ✗

    year + '-' + month

    Why it's wrong here

    Using `year + '-' + month` attempts to concatenate an integer with a string using the `+` operator. However, Python's string concatenation requires both operands to be strings, so this raises a `TypeError` for unsupported operand types. Even though the hyphen is a string, the integer operands cannot be implicitly converted to strings during concatenation.

  • ✗

    str(year) + '-' + str(month)

    Why it's wrong here

    `str(year) + '-' + str(month)` explicitly converts both integers to strings, avoiding a TypeError, but it does not add zero-padding to the month. For months 1 through 9, `str(month)` yields a single digit, so a date like March becomes `2024-3` instead of the expected `2024-03`. The resulting string is therefore not consistently in the desired two-digit-month format.

  • ✗

    '%s-%s' % (year, month)

    Why it's wrong here

    The old-style `%` formatting in `'%s-%s' % (year, month)` substitutes each `%s` with the string representation of the corresponding value, so it accepts integers without an error. However, `%s` is a generic string conversion and never performs numeric formatting, meaning the month will not be zero-padded. Thus, as with `str()`, the month appears as `1`, `2`, ..., `9` rather than `01` through `09`.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.