Courseiva
Strings →easyMultiple Select

PCAP Strings Practice Question

Which TWO of the following are valid ways to use string formatting in Python? (Choose two.)

⚠ Common exam trap

The PCAP exam often tests the distinction between old-style `%` formatting and new-style `.format()` by including plausible but syntactically incorrect variants like `$s` or mismatched placeholders, trapping candidates who rely on memorization rather than precise syntax knowledge.

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

✓

"Hello {0}".format("World")

The `str.format()` method with positional index `{0}` is a valid way to insert the argument `"World"` into the string, producing `"Hello World"`. This is a core feature of Python's string formatting, introduced in Python 2.6, and is explicitly tested in the PCAP exam.

Answer analysis

Option-by-option breakdown

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

  • ✓

    "Hello {0}".format("World")

    Why this is correct

    This is a valid use of the `str.format()` method. The replacement field `{0}` refers to the first positional argument passed to `.format()`, which is the string `"World"`. Python substitutes `"World"` for `{0}`, producing `"Hello World"`; zero-based indexing means `{0}` is the first argument, so this line works correctly.

  • ✗

    "Hello $s" % "World"

    Why it's wrong here

    This line is invalid because the `%` operator does not recognize `$s` as a conversion specifier. The old-style `%` formatting only accepts C-style placeholders such as `%s`, `%d`, `%f`, etc. Since `$` is not a valid format character, the `"World"` argument is not substituted, leading to a `TypeError` (because the format string has no valid specifier) or leaving the literal text unmodified—either way, it does not produce the intended output.

  • ✗

    f"Hello {world}"

    Why it's wrong here

    This f-string will raise a `NameError` unless a variable named `world` is defined in the current scope. F-strings evaluate the expression inside `{}` immediately at runtime, so `{world}` requires an existing variable; unlike positional `.format()` fields or `%s`, there is no automatic substitution of a literal string. Without a defined `world`, the code fails, so this is not reliably a valid way to format a literal.

  • ✓

    "Hello %s" % "World"

    Why this is correct

    This is a valid example of old-style `%` formatting. The `%s` placeholder in the format string is replaced by the value to the right of `%`; here, the string `"World"` is converted to a string and substituted for `%s`, yielding `"Hello World"`. The `%s` operator is a classic Python formatting mechanism, and this line correctly demonstrates its use.

  • ✗

    "Hello {name}".format("World")

    Why it's wrong here

    This call to `.format()` raises a `KeyError`. The replacement field `{name}` is a keyword-based name, but only one positional argument (`"World"`) is supplied, and no keyword argument `name` is provided. Unlike `{0}` which selects a positional argument, `{name}` requires a matching `name=` keyword argument; since it is absent, the method cannot resolve the field and throws an error.

About these practice questions

This PCAP question is part of Courseiva's 421-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.