PCEP-30-02 · Question #273
Which of the following operators can be used with strings? `` 1) + 2) * 3) - 4) in ``
The correct answer is B. 1, 2, 4. Option B is correct because +, `, and in are all valid string operators in Python, while - is not. The + operator concatenates strings ("hello" + " world" → "hello world"), repeats a string a given number of times ("ha" 3 → "hahaha"), and in tests for substring membership…
Question
1) +
2) *
3) -
4) in
Options
- A1, 2, 3
- B1, 2, 4
- C1, 2, 3, 4
- D1, 2
How the community answered
(43 responses)- A2% (1)
- B79% (34)
- C14% (6)
- D5% (2)
Explanation
Option B is correct because +, *, and in are all valid string operators in Python, while - is not. The + operator concatenates strings ("hello" + " world" → "hello world"), * repeats a string a given number of times ("ha" * 3 → "hahaha"), and in tests for substring membership ("ell" in "hello" → True). The subtraction operator (-) has no defined behavior for strings and raises a TypeError at runtime, which is why options A and C (which include -) are wrong, and option D is wrong for omitting in. A useful memory tip: think of strings as sequences you can add together, multiply for repetition, and search within - but you can never subtract from a sequence, so - is the odd one out.
Community Discussion
No community discussion yet for this question.