w3resource

Python: Reverse a string word by word

Python Class: Exercise-8 with Solution

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:

Python: Reverse a string word by word.

Flowchart:

Flowchart: Reverse a string word by word

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/class-exercises/python-class-exercise-8.php