Question: Find the output of the following Python program.
def display(s):
l = len(s)
m=""
for i in range(0,l):
if s[i].isupper():
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
elif s[i].isdigit():
m=m+"$"
else:
m=m+"*"
print(m)
display("EXAM20@cbse.com")
Code language: Python (python)
Output:
exam$$*CBSE*COM
Read Also: How To Check String Contains Special Characters In Java?
Explanation:
In the above python program, we have the given String ”EXAM20@cbse.com”.
First, we have found the length of the given string by using the len( ) function and we have declared one empty string m=””. Then we have used for loop iteration.
Iteration-1:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = “” i = 0 | As i = 0 and s[0] = E, So m=m + s[i].lower() m = “” + s[0].lower() = “” + e = e |
Iteration-2:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = e i = 1 | As i = 1 and s[1] = X, So m = m + s[i].lower() m = e + s[1].lower() = e + x = ex |
Iteration-3:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = ex i = 2 | As i = 2 and s[2] = A, So m = m + s[i].lower() m = ex + s[2].lower() = ex + a = exa |
Iteration-4:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exa i = 3 | As i = 3 and s[3] = M, So m = m + s[i].lower() m = exa + s[3].lower() = exa + m = exam |
Iteration-5:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam i = 4 | As i = 4 and s[4] = 2, So m = m+”$” m = exam + “$” = exam$ |
Iteration-6:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$ i = 5 | As i = 5 and s[5] = 0(digit), So m = m+”$” m = exam$ + “$” = exam$$ |
Iteration-7:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$ i = 6 | As i = 6 and s[6] = @(Special Character), So m = m+”*” m = exam$$ + “*” = exam$$* |
Iteration-8:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$* i = 7 | As i = 7 and s[7] = c (Small Alphabet), So m = m + s[i].upper() m = exam$$* + s[7].upper() = exam$$* + C = exam$$*C |
Iteration-9:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*C i = 8 | As i = 8 and s[8] = b (Small Alphabet), So m = m + s[i].upper() m = exam$$*C + s[8].upper() = exam$$*C + B = exam$$*CB |
Iteration-10:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*CB i = 9 | As i = 9 and s[9] = s (Small Alphabet), So m = m + s[i].upper() m = exam$$*CB + s[9].upper() = exam$$*CB + S = exam$$*CBS |
Iteration-11:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*CBS i = 10 | As i = 10 and s[10] = e (Small Alphabet), So m = m + s[i].upper() m = exam$$*CBS + s[10].upper() = exam$$*CBS + E = exam$$*CBSE |
Iteration-12:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*CBSE i = 11 | As i = 11 and s[11] = . (Special Character), So m = m + “*” m = exam$$*CBSE + “*” = exam$$*CBSE* |
Iteration-13:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*CBSE* i = 12 | As i = 12 and s[12] = c (Small Alphabet), So m = m + s[i].upper() m = exam$$*CBSE* + s[12].upper() = exam$$*CBSE* + C = exam$$*CBSE*C |
Iteration-14:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*CBSE*C i = 13 | As i = 13 and s[13] = o (Small Alphabet), So m = m + s[i].upper() m = exam$$*CBSE*C + s[13].upper() = exam$$*CBSE*C + O = exam$$*CBSE*CO |
Iteration-15:
Current Values | Operation |
s = EXAM20@cbse.com l = 15 m = exam$$*CBSE*CO i = 14 | As i = 14 and s[14] = m (Small Alphabet), So m = m + s[i].upper() m = exam$$*CBSE*CO + s[14].upper() = exam$$*CBSE*CO + M = exam$$*CBSE*COM |
So as per the above iteration-15, the value of m will be exam$$*CBSE*COM. Finally, by using the print method( print(m) ), we have displayed the output to the user.