PCAP Strings Practice Question
A developer is working on a logging system where dynamic values are inserted into a template string. The template is 'User %s logged in at %s'. The developer has the username and timestamp as separate variables. Which approach is most Pythonic (PEP 498) and recommended for new code?
⚠ Common exam trap
The PCAP exam often tests the distinction between 'most Pythonic' and 'works correctly' — candidates may pick .format() because it is familiar, but PEP 498 explicitly recommends f-strings for new code, making them the correct answer in a PCAP context.
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
✓
Use an f-string: f'User {username} logged in at {timestamp}'
PEP 498 introduced f-strings (formatted string literals) as the recommended approach for string formatting in Python 3.6+. They are concise, readable, and evaluated at runtime, allowing direct embedding of expressions. This aligns with the 'Pythonic' principle of simplicity and is the preferred style for new code according to the official Python documentation.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use %-formatting: 'User %s logged in at %s' % (username, timestamp)
Why it's wrong here
The %-formatting style is a valid way to inject values, but it is considered legacy because it relies on printf-style placeholders that require a tuple of arguments and literal type conversions. It becomes unwieldy when arguments must be reordered or reused, and mistakes such as passing a non-tuple or wrong type can raise TypeError at runtime. Modern Python code should prefer f-strings for their readability and direct expression evaluation.
- ✗
Use .format(): 'User {} logged in at {}'.format(username, timestamp)
Why it's wrong here
The .format() method is a capable string-formatting tool that supports reordering fields, named replacements, and full format specifications. However, when username and timestamp are already in scope, calling .format() adds an unnecessary method call and forces you to repeat the variable names inside the braces, which obscures the message and reduces concision. F-strings achieve the same result with inline interpolation and fewer syntactic distractions.
- ✗
Concatenate: 'User ' + username + ' logged in at ' + timestamp
Why it's wrong here
Concatenating strings with the + operator builds a new string at runtime, but it requires every operand to already be a string; if username is not a string or timestamp is a datetime object, Python raises a TypeError unless you explicitly convert them with str(). This style is also less readable for longer templates because it scatters the constant text and variables, and it is inefficient for combining many parts into a single string.
- ✓
Use an f-string: f'User {username} logged in at {timestamp}'
Why this is correct
The f-string (formatted string literal) is the recommended formatting method in Python 3.6+ because it allows expressions to be embedded directly inside braces exactly where the value belongs in the text. It is concise, readable, and evaluated at runtime, so it can call functions, index collections, or access attributes without extra method calls. PEP 498 and the official Python documentation endorse f-strings as the preferred form for new code.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 169-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 →
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.