Exam-style question
Try this first
A program receives a string called code containing a three-character code. The first character represents a category, and the final two characters represent a number. Describe a sequence of string-handling operations that extracts the category and number, converts the number to an integer, then creates a message containing the category and the number converted back to a string. Use Python 3 examples where helpful.
Model answer
What a good answer should say
- The program can find the length of code if a length check is required, then use substring operations to extract the first character and the final two characters.
- In Python 3, category = code[0] extracts the first character and numberText = code[1:3] extracts the final two characters.
- The program can convert numberText to an integer with number = int(numberText).
- After any required numeric processing, it can convert the integer back to a string with numberText2 = str(number).
Explanation
Why this works
A complete response identifies the relevant positions, extracts the two parts using substring operations, uses string-to-integer conversion before treating the numeric part as a number, and uses integer-to-string conversion before concatenating it into the final message. Concatenating an integer directly with a string would not be a valid string-concatenation step in Python 3.
Common mistake
No common mistake is linked to this question yet.
