Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

A developer writes a log message with variables: name = 'Alice' and age = 30. Which of the following uses an f-string correctly?

⚠ Common exam trap

The PCAP exam often tests the exact syntax of f-strings, specifically that the 'f' prefix must be immediately followed by a string literal (no space, no dot, no parentheses), and that the expressions inside curly braces are evaluated in the current scope.

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'Name: {name}, Age: {age}'

It uses the proper f-string syntax: the letter 'f' immediately followed by a string literal (single or double quotes) containing expressions in curly braces. In Python, f-strings (formatted string literals) evaluate expressions inside {} and insert them into the string at runtime, making them concise and readable.

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(Name: {name}, Age: {age})

    Why it's wrong here

    The f-prefix must be immediately followed by a string literal delimiter (a single or double quote) to form a formatted string literal. Here, placing parentheses after the f yields an identifier 'f' followed by a parenthesized expression, but the content 'Name: {name}, Age: {age}' is not a valid Python expression because of the colons and stray identifiers. Python never creates a string, so no f-string interpolation occurs — this is a syntax error, not an f-string.

  • ✓

    f'Name: {name}, Age: {age}'

    Why this is correct

    This is a correctly formed f-string: the f prefix is directly attached to a single-quoted string literal, and each pair of curly braces contains an expression to be evaluated at runtime. Python inserts the values of the variables 'name' and 'age' into the string, converting them with their __format__ methods. The single quotes wrap the entire literal, making the syntax valid per PEP 498.

  • ✗

    'Name: %s, Age: %d' % (name, age)

    Why it's wrong here

    This line uses the older printf-style string formatting, where '%s' and '%d' are placeholders filled by the values supplied after the % operator. Although it produces the same logged output as an f-string, the question specifically asks for an f-string, which requires a leading 'f' prefix and curly-brace interpolation. This is functionally valid Python, but it is not an f-string, so it fails the requirement.

  • ✗

    f.'Name: {name}, Age: {age}'

    Why it's wrong here

    The dot between the f and the opening quote is invalid: in Python, a string prefix must be a single character like f, r, or b written without any punctuation or whitespace before the quote. The sequence 'f.' causes the tokenizer to see an identifier 'f' followed by a dot, which would normally indicate attribute access, but a following quote creates a syntax error. No f-string literal is ever constructed, so this line cannot compile.

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.