Q. Find and write the output of the following python code:


def fun(s):

    k = len(s)

    m = ""

    for i in range(0,k):

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

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

        elif s[i].isalpha():

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

        else:

            m = m + 'bb'

    print(m)


fun('school2@com')


Answer :-

Output :-

SCHOOLbbbbCOM
>>>

Explanation :-

for good explanation I have write this code in python :-

def fun(s):
    k = len(s)
    print ( "k =", k )
    m = ""
    print ( "m =", m )
    print ( )
    print ( "Now Loop start working" )
    print ( )
    for i in range(0,k):
        print ( "i =", i )
        if(s[i].isupper()):
            m = m+s[i].lower()
        elif s[i].isalpha():
            m = m+s[i].upper()
        else:
            m = m + 'bb'
        print("m = ", m)
        print()
    print("So for is Output :-")
    print(m)

fun('school2@com')

The output will definitely clear your all doubts.

k = 11
m = 

Now Loop start working

i = 0
m =  S

i = 1
m =  SC

i = 2
m =  SCH

i = 3
m =  SCHO

i = 4
m =  SCHOO

i = 5
m =  SCHOOL

i = 6
m =  SCHOOLbb

i = 7
m =  SCHOOLbbbb

i = 8
m =  SCHOOLbbbbC

i = 9
m =  SCHOOLbbbbCO

i = 10
m =  SCHOOLbbbbCOM

So for is Output :-
SCHOOLbbbbCOM

>>> 


11 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Explain please

    ReplyDelete
  2. Sir, is the value of @ not taken

    ReplyDelete
  3. Sir ,is it because there is no input in the function for changing value of special characters like @

    ReplyDelete
    Replies
    1. No, here special characters convert into 'bb'.

      Delete
  4. Sir could you plz gives a video explaining it will help us to understand it better way

    ReplyDelete
  5. If(s[i].isupper()): how is this line executed when school2@com is written in lowercase?

    ReplyDelete
    Replies
    1. it execute in "elif s[i].isalpha()" statement.

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post