Q.Find and write the output of the following python code: Msg1="WeLcOME" Msg2="GUeSTs" Msg3="" for I in range(0,len(Msg2)+1): if Msg1[I]>="A" and Msg1[I]<="M": Msg3=Msg3+Msg1[I] elif Msg1[I]>="N" and Msg1[I]<="Z": Msg3=Msg3+Msg2[I] else: Msg3=Msg3+"*" print Msg3
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 loop walks all 7 characters of Msg1 = "WeLcOME". Uppercase A–M characters are copied as-is, uppercase N–Z characters are replaced by the same-index character of Msg2 = "GUeSTs", and every lowercase character falls to the else and becomes *. The Python-2 print Msg3 statement displays G*L*TME.
Concept — why lowercase letters always become *
This is Python-2 era code (print Msg3 is the Python 2 print statement). String comparisons use character codes: uppercase letters occupy 65–90 ('A'–'Z') and lowercase letters occupy 97–122 ('a'–'z'). A lowercase letter like 'e' (code 101) satisfies 'e' >= "N" but fails 'e' <= "Z" because 101 > 90 — so no lowercase letter can pass either uppercase band test, and every lowercase character lands in the else branch and appends *.
The loop bound is range(0, len(Msg2)+1). len("GUeSTs") is 6, so the loop runs for I = 0, 1, 2, 3, 4, 5, 6 — exactly the 7 indices of Msg1 = "WeLcOME".
The three branches:
"A" <= Msg1[I] <= "M"→ appendMsg1[I]itself,"N" <= Msg1[I] <= "Z"→ appendMsg2[I](same index, other string),- otherwise → append
"*".
Step-by-step trace
| I | Msg1[I] | In A–M? | In N–Z? | Branch taken | Character appended | Msg3 after |
|---|---|---|---|---|---|---|
| 0 | W | no | yes | elif | Msg2[0] = G | G |
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.