PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A junior developer writes: print('Hello' + 5). This code raises an error. What is the best way to fix it?
⚠ Common exam trap
Python Institute often tests the distinction between concatenation (`+`) and multiple arguments in `print()`, where candidates may think `print('Hello', 5)` produces `Hello5` instead of `Hello 5` due to the default `sep=' '` parameter.
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
✓
print('Hello' + str(5))
Python's `+` operator for strings requires both operands to be strings. The code `print('Hello' + 5)` raises a `TypeError` because it attempts to concatenate a string with an integer. Using `str(5)` converts the integer to the string `'5'`, allowing the concatenation to succeed and print `Hello5`.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
print('Hello' * 5)
Why it's wrong here
The multiplication operator repeats the string, producing 'HelloHelloHelloHelloHello' rather than concatenating the number, so it changes the intended output entirely. It would be correct only if repetition of the text were genuinely wanted, not joining 'Hello' with the value 5.
- ✓
print('Hello' + str(5))
Why this is correct
Explicitly converting the integer with str() resolves the TypeError, because Python's + operator requires both operands to be the same sequence type for concatenation. It satisfies the stem's constraint of fixing the error while preserving the original concatenation intent, yielding 'Hello5' rather than altering the output format.
- ✗
print('Hello', 5)
Why it's wrong here
Passing 5 as a separate argument inserts a space and relies on print's default separator, so it does not concatenate the string and number as intended. It is tempting because print accepts mixed types, but the required fix is converting 5 with str() before concatenation.
- ✗
print('Hello' + '5')
Why it's wrong here
Wrapping 5 in quotes concatenates the literal character '5', yielding 'Hello5' — the string, not the number — so the numeric value is lost. This would be correct only if the digit were meant as text, such as building a label or identifier.
Go deeper
Related to this question
About these practice questions
One of 482 original PCEP 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 →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP 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 PCEP exam.