Python: Reverse a string word by word
8. Reverse a String Word by Word
Write a Python class to reverse a string word by word.
Sample Solution:
Python Code:
class py_solution:
def reverse_words(self, s):
return ' '.join(reversed(s.split()))
print(py_solution().reverse_words('hello .py'))
Sample Output:
.py hello
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python class that splits a string into words and then reconstructs it in reverse order.
- Write a Python class that uses slicing to reverse the order of words in a given string.
- Write a Python class that implements a method to reverse each word's order and then reverse the word sequence.
- Write a Python class that uses recursion to reverse the order of words in a sentence and print the output.
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python class to implement pow(x, n).
Next: Write a Python class which has two methods get_String and print_String. get_String accept a string from the user and print_String print the string in upper case.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics