For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Reverse words order and swap cases using Python

Reverse words order and swap cases using Python

Reverse words order and swap cases using Python
Input:

Hello World

Output:

wORLD hELLO

The function is expected to return a STRING

The function accepts STRING sentence as parameter.

def reverse_words_order_and_swap_cases(sentence):
    mystr = ''
    for i in sentence:
        if i.isspace():
            mystr+= " "
        else:
            if i.isupper():
                mystr+= i.lower()
            if i.islower():
                mystr+= i.upper()
    words = list(reversed(mystr.split()))
    mywords = " ".join(words)
    return mywords
    
out = reverse_words_order_and_swap_cases("Hello World")
print(out)

In above program:

isupper() method is used to check the given letter is capital or not.

islower() method is used to check the given letter is small or not.

lower() method is used to convert string or character to small.

upper() method is used to convert string or character to capital.

The reversed() function returns an iterator that accesses the given sequence in the reverse order.