Practice Python

follow us in feedly Atom feed XML

Beginner Python exercises

28 May 2014

Reverse Word Order Solutions

Exercise 15

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:

  My name is Michele

Then I would see the string:

  Michele is name My

shown back to me.

Sample solution

Here is the quick, one-liner solution to the problem:

But most likely you didn’t come up with that solution right away. You most likely went through a number of iterations like this:

But you also could have taken a hybrid approach:

comments powered by Disqus