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()
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
-
Initialization:
S = "Racecar Car Radar"andL = S.split()produces the listL = ["Racecar", "Car", "Radar"]. -
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
xand print with*:R*A*C*E*C*A*R* - The
print()at the end adds a newline.
- Convert to uppercase:
-
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# ``` …
- Convert to uppercase:
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.