Question Detail

COS 102 Python strings Objective Question

Question

To correctly combine the string "Age: " with the integer 20, which of the following Python expressions should be used?
Options
A "Age: " - 20
B "Age: " + int(20)
C "Age: " + str(20)
Correct Answer
D str("Age: ") + 20
Correct Answer

Option C is the correct answer.

Detailed Explanation

Page 10, under 'The Fix', provides the solution: 'Convert the number to a string first using str().' So, "Age: " + str(20) is the correct approach.

Hint

Remember that both operands of the '+' operator must be of the same type for string concatenation. How do you convert an integer to a string?

Question Info