Courseiva
Strings →easyMultiple Choice

PCAP f-string Practice Question

Exhibit

Refer to the exhibit.

```
# File: config.py
name = "Alice"
age = 30
print(f"{name} is {age} years old.")
```

Consider the following code:

name = "Alice" age = 30

print(f"{name} is {age} years old.")

What is the output of the above code?

⚠ Common exam trap

Python Institute often tests whether candidates notice subtle punctuation differences (like missing commas or extra periods) in f-string output, tricking those who focus only on the variable values and ignore exact string formatting.

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

✓

Alice is 30 years old.

Given the code: name = "Alice" age = 30 print(f"{name} is {age} years old.") The f-string correctly interpolates the variables, producing 'Alice is 30 years old.' Option D is correct. Option A incorrectly includes an extra comma after 30. Option B uses the literal string 'Name' instead of the variable name. Option C adds an extra period at the end.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Alice is 30, years old.

    Why it's wrong here

    The format string after {age} contains only a space and then 'years old.', so no comma appears between the number and the word 'years'. The placeholder {age} is replaced with the integer's value 30 and does not introduce any extra punctuation. Seeing '30,' implies that a comma was added in the literal portion of the format string, which is not part of the correct code.

  • ✗

    Name is 30 years old.

    Why it's wrong here

    This output would occur if the placeholder braces were absent or if the string were written as a plain string literal 'Name is 30 years old.' without f-string interpolation. In an actual f-string, {name} substitutes the value of the variable `name`, which is 'Alice', not the word 'Name'. The invalid option treats the placeholder name as the output text, which ignores Python's evaluation of expressions inside braces.

  • ✗

    Alice is 30 years old..

    Why it's wrong here

    The f-string's literal suffix is ' years old.' which already provides the single terminal period after the interpolated value. Selecting an option with two consecutive periods indicates an extra period was appended to the output, either as an additional literal character or by using string concatenation with another period. Since the format string in the code contains exactly one period, the printed result cannot contain two unless the source code itself was modified.

  • ✓

    Alice is 30 years old.

    Why this is correct

    In an f-string, expressions inside braces are evaluated and replaced with their string representation, so {name} becomes 'Alice' and {age} becomes '30'. The surrounding literal text ' is ' and ' years old.' is preserved verbatim, giving the exact concatenation 'Alice is 30 years old.'. This matches the expected output because the variable `name` holds the string 'Alice', and the integer `age` is 30.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.