Skip to content
Question 56 of 97

Q.Write the output on execution of the following Python code: S="Racecar Car Radar" L=S.split() for W in L: x=W.upper() if x==x[::-1]: for I in x: print(I,end="*") else: for I in W: print(I,end="#") print()

Punjab PsebCBSE Class XII Board 2024Subjective· 3mImportance★★★★★
58% · 56/97 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The code splits the string into words, checks each word (in uppercase) for palindrome status, then prints characters with * separators if palindrome or # separators otherwise, followed by a newline.

Concept: String manipulation and palindrome detection

The code demonstrates three core Python ideas working together: string splitting, palindrome checking via slice reversal, and conditional character-by-character output. A palindrome reads the same forwards and backwards; the slice [::-1] reverses a string, so comparing x == x[::-1] tests this property. The key insight is that the palindrome check uses the uppercase version but the output format depends on whether that check passes.

Let's trace through the execution step by step.

Execution trace

  1. Initialization: S = "Racecar Car Radar" and L = S.split() produces the list L = ["Racecar", "Car", "Radar"].

  2. First iteration (W = "Racecar"):

    • Convert to uppercase: x = "RACECAR"
    • Check palindrome: "RACECAR" == "RACECAR"[::-1] → "RACECAR" == "RACECAR" → True
    • Since it's a palindrome, loop through each character in x and print with *:
      R*A*C*E*C*A*R*
      
    • The print() at the end adds a newline.
  3. Second iteration (W = "Car"):

    • Convert to uppercase: x = "CAR"
    • Check palindrome: "CAR" == "RAC" → False
    • Since it's not a palindrome, loop through each character in W (original case) and print with #:
      C#a#r#
      ``` …
      

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.