Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. For example, say I type the string:
Then I would see the string:
shown back to me.
Concepts for this week:
Python has a lot of interesting things you can do with strings. I will show a few here, but you can see many more methods that may or may not be useful at the official Python documentation about the string format.
Remember that strings are lists.
You can “split” or tear apart strings based on a given set of characters. For example:
And at the end,
result will contain the list:
"t", you can write any character you want. If you do not include any character, it means “split on all whitespace”:
You can also relatively easily “join” or “smush” strings together:
result will contain the string:
Take a look at the official Python documentation for more information.
Forgot how to submit exercises?comments powered by Disqus