nerdexam
Python_Institute

PCEP-30-02 · Question #150

What is the expected output of the following code? `` x = '\\\' print(len(x)) ``

The correct answer is C. 2. Option C is correct because Python processes escape sequences before creating the string object: \\ collapses to a single backslash \, and \' collapses to a single quote ', yielding a two-character string \' at runtime - so len(x) returns 2. Option A (4) is a classic trap - it…

Question

What is the expected output of the following code?
x = '\\\'
print(len(x))

Options

  • A4
  • BThe code is erroneous.
  • C2
  • D1

How the community answered

(60 responses)
  • A
    13% (8)
  • B
    7% (4)
  • C
    77% (46)
  • D
    3% (2)

Explanation

Option C is correct because Python processes escape sequences before creating the string object: \\ collapses to a single backslash \, and \' collapses to a single quote ', yielding a two-character string \' at runtime - so len(x) returns 2.

Option A (4) is a classic trap - it counts the raw source characters between the delimiters (\, \, \, ') instead of the resolved characters after escape processing. Option B (erroneous) tempts those who misread the quote structure, but the final ' in '\\\'' acts as the closing delimiter because \\ already consumed the first two backslashes, making the string syntactically valid. Option D (1) partially applies the rules - it correctly reduces \\ to one backslash but forgets that the \' following it also collapses to one character.

Memory tip: Think of each escape pair (\\, \', \n, etc.) as a 2-for-1 coupon - two source characters buy you exactly one runtime character. Count the coupons, not the raw characters.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice