Question Detail

COS 102 Python strings Objective Question

Question

What happens if you try to concatenate a string and a number directly in Python, for example, "Age: " + 20?
Options
A Python automatically converts the number to a string and concatenates them.
B The operation results in a TypeError.
Correct Answer
C The number is ignored, and only the string is output.
D Python performs arithmetic addition if the string contains a number.
Correct Answer

Option B is the correct answer.

Detailed Explanation

The document clearly states under 'Avoid the TypeError' that 'You cannot add a string and a number directly,' leading to an 'Error!' (specifically, a TypeError).

Hint

Look at the 'Avoid the TypeError' section and the 'Mixing Types' example.

Question Info