Test Index

Python Revision Tour Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 76 of 102
Marks: +1, -0
Predict the output of the Python code given below :
s="India Growing"
n = len(s)
m=""
for i in range (0, n) :
if (s[i] >= 'a' and s[i] <= 'm') :
m = m + s [i].upper()
elif (s[i] >= 'O' and s[i] <= 'z') :
m = m +s [i-1]
elif (s[i].isupper()):
m = m + s[i].lower()
else:
m = m + '@'
print (m)
[CBSE 2024 Compartment]
Solution:  
Concept:
Characters are compared by their ASCII/Unicode values: 'A'-'Z' are 65-90, 'a'-'z' are 97-122, space is 32. So 'O' <= ch <= 'z' is true for 'O'-'Z', a few symbols, and all lowercase letters; since 'a'-'m' are already caught by the first test, the second test catches 'n'-'z' (and uppercase 'O'-'Z').
Explanation: s = "India Growing", n = 13
is[i]BranchAdded
0Iuppercase, before Oi
1n'O' to 'z': add s[0]I
2d'a' to 'm': upperD
3i'a' to 'm': upperI
4a'a' to 'm': upperA
5(space)else@
6Guppercase, before Og
7r'O' to 'z': add s[6]G
8o'O' to 'z': add s[7]r
9w'O' to 'z': add s[8]o
10i'a' to 'm': upperI
11n'O' to 'z': add s[10]i
12g'a' to 'm': upperG
Output:
iIDIA@gGroIiG
© examsnet.com
Advertisements
Go to Question: