Course Content
What is Python?
Introduction of Python and Its setup
0/2
Control Statement
Control statements are used to control the flow of execution depending upon the specified condition/logic.
0/4
File Handling
File handling is an important component of any application. Python has multiple functions for creating, reading, updating, and deleting files.
0/2
Examples
Following are the examples of python scripts to try hands-on, you are ready to start your python journey.
0/7
Python
About Lesson

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 a 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.