How to Fix TypeError When Concatenating String and Integer
Exhibit
Error log:
Traceback (most recent call last):
File "test.py", line 3, in <module>
print('Hello' + 5)
TypeError: can only concatenate str (not "int") to strRefer to the exhibit. Which of the following fixes the error?
Quick Answer
The correct answer is both A and B, as each resolves the string concatenation type error by ensuring the integer is treated as a string before the plus operator is applied. In Python, the `+` operator only concatenates two strings; attempting to combine a string with an integer raises a TypeError because the interpreter cannot implicitly convert the integer to a string. Option A directly concatenates the string literal `'5'`, while Option B explicitly converts the integer `5` using `str()`, both producing `'Hello5'` without error. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of type coercion and the strictness of Python’s dynamic typing—a common trap is assuming Python will auto-convert integers to strings during concatenation. Remember the memory tip: “Strings stick to strings; numbers need a `str()` ring.”
⚠ Common exam trap
Python Institute often tests the distinction between implicit type conversion (which Python does not do for string+int) and explicit conversion using str(), and candidates may forget that string repetition with * is valid but does not solve a concatenation error.
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
✓
Both A and B
Both A and B produce the string 'Hello5' without error. In Python, the + operator concatenates strings, so 'Hello' + '5' works. Option B converts the integer 5 to a string using str() before concatenation, which also works. Option D uses the * operator to repeat the string 'Hello' five times, producing 'HelloHelloHelloHelloHello', which is a valid operation but does not fix the error described in the exhibit (likely a TypeError from trying to concatenate a string and an integer).
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
This statement is syntactically correct and will print 'Hello5', but it is not the correct answer because the question asks which option fixes the error, and both A and B fix it, so selecting only A is incomplete.
- ✗
print('Hello' + str(5))
Why it's wrong here
Similarly, this statement works correctly by converting the integer 5 to a string, but it is only one of the valid fixes. The correct answer is C, which includes both A and B.
- ✓
Both A and B
Why this is correct
Both A and B produce the intended output without error. Option A uses direct string concatenation with a string literal, while option B uses explicit conversion of the integer to a string. Neither causes a TypeError, so both fix the error described in the exhibit.
- ✗
print('Hello' * 5)
Why it's wrong here
This statement uses the repetition operator to repeat the string 'Hello' five times, resulting in 'HelloHelloHelloHelloHello'. While syntactically valid, it does not address the error of concatenating a string and an integer, as it does not involve concatenation with the number 5.
Go deeper
Related to this question
About these practice questions
One of 169 original PCAP 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 →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Refer to the exhibit. What is the output?
medium- A.IndexError
- B.No output
- C.Hello World
- ✓ D.TypeError
Why D: The code in the exhibit attempts an operation that raises a TypeError, such as using the '+' operator between a string and an integer without proper conversion. In Python, this raises a TypeError: can only concatenate str (not 'int') to str. The other options are not consistent with the expected behavior.
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.