Reverse Words in a Given String in Python Last Updated : 27 Oct, 2025 Comments Improve Suggest changes 73 Likes Like Report Given a string, the task is to reverse the words in it without changing the words themselves. For example, for "Learning Python is easy", the reversed string would be "easy is Python Learning" (words in reverse order). Using split() and join()This method splits the string into words using split(), reverses the list of words and then joins them back into a single string using join(). Python s = "Python is fun" res = ' '.join(s.split()[::-1]) print(res) Outputfun is Python Explanation:s.split() splits the string into a list of words.[::-1] reverses the list of words.' '.join(...) concatenates the reversed words into a single string separated by spaces.Using a LoopThis method manually iterates through the list of words in reverse order, building the reversed string step by step. It is helpful when you want to control the process explicitly. Python s = "Python is fun" words = s.split() res = "" for word in reversed(words): res += word + " " res = res.strip() print(res) Outputfun is Python Explanation:s.split() splits the string into a list of wordsreversed(words) creates an iterator to traverse the list in reverse order.Inside the loop, res += word + " " appends each word followed by a space.res.strip() removes the trailing space at the end.Using deque from collectionsA deque (double-ended queue) is efficient for large strings, as it allows O(1) pops from both ends. This method uses pop() to retrieve words in reverse order. Python from collections import deque s = "Python is fun" words = deque(s.split()) res = "" while words: res += words.pop() + " " res = res.strip() print(res) Outputfun is Python Explanation:deque(s.split()) creates a deque for fast popping from both ends.words.pop() removes and returns the last word from the deque.res += ... appends the word followed by a space.res.strip() removes trailing space.Using StackThis method uses a stack (LIFO structure) to reverse the order of words. Words are pushed onto the stack and popped to get them in reverse order. Python s = "Python is fun" words = s.split() stack = [] for word in words: stack.append(word) res = "" while stack: res += stack.pop() + " " res = res.strip() print(res) Outputfun is Python Explanation:stack.append(word) pushes each word onto the stack.stack.pop() removes the last word pushed, giving reversed order. Create Quiz Comment S Shashank Mishra Follow 73 Improve S Shashank Mishra Follow 73 Improve Article Tags : Python Python string-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like