(a) Predict the output of the code given below:

s="welcome2cs"

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] >= 'n' and s[i] <= 'z'):

m = m +s[i-1]

elif (s[i].isupper()):

m = m + s[i].lower()

else:

m = m +'&'

print(m)

 

Answer:

Question 32 Choice 2 a -Teachoo.png

Output: sELCcME&Cc

Explanation:

The above code takes the string s and iterates through each character of the string and:

  • If the character is between ‘a’ and ‘m’, it is changed to uppercase and appended to the string m.
  • Else if the character is between ‘n’ and ‘z’, its preceding character in the string is appended to the string m.
  • Else if the character is in upper case, it is changed to smaller case and appended to the string m.
  • Else, if the character does not satisfy any of the above conditions, an ‘&’ symbol is appended to the string m.

The final string ‘m’ is then printed as output. 

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.