Reverse Word Order
Lesson: Reverse Words
Create a function that
- takes a single string argument and
- returns a string with the words of the original argument in reversed order
E.g. when given "Mary says hi"
the function returns "hi says Mary"
.
Optionally: Create a Python script reverse_words.py
that takes a string command line argument and prints the words of this argument in reversed order. E.g. when executed as
python3 reverse_words.py "Mary says hi"
the program prints
"hi says Mary"
.
Optionally: Add options to the function and the script that allow for returning/printing the result string
-
You can access the command line arguments through
sys.argv
. -
Python features an extensive command line parsing library in the stlib:
argparse
.- reversed or unreversed and that in
- all-uppercase, all-lowercase, titlecase or capitalized.
-
Use
help("")
orhelp(str)
to learn useful string methods.