Reverse Words in a String Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

We are given a string as an input in Python, and our task is to reverse words present in the string. We will use different string functions in Python to complete the program.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

How To Reverse Words In A String Python?

In Python, a string is created by putting a stream of characters inside single quotes, double quotes, or triple quotes. Reverse words in a string mean that we have to reverse the position of all words in the given string.

Examples

Let us look at some examples :

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Method - 1: Reverse The Individual Words And Then Reverse The Whole String

The intuition of this approach is to first reverse every word present in the string individually,

For Example:
I like coding becomes, I ekil gnidoc. Now, we have to reverse the whole string to get the desired result which in the case of this example will be coding like I.

Let us now see the implementation of this approach to reverse words in a string in Python :

Output:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Complexity analysis

Since we are traversing the whole string once to reverse the words in a string, the Time Complexity is O(n)O(n), where nn is the size of the string.

While an auxiliary space is required to reverse words in a string, the Space Complexity is O(n)O(n), where nn is the size of the string.

Method - 2: By Splitting The String Using The split() Function In Python

The above-discussed method does not handle the case when the string starts with a space. Therefore, we will discuss an approach using Python's in-built split() function. The algorithm to implement this method is as follows :

  1. Define a function that will take a string as input.
  2. Split the input string on space using the split() function and store the result as a list.
  3. Reverse the created list using the reversed() function and add them to a new string using the join() string function in Python.
  4. Return the new string

Let us now see the implementation of this approach to reverse words in a string in Python :

Output :

Python's built-in reversed() function returns an iterator object rather than an entire list.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Complexity analysis

Since we are traversing the whole string to split it in space, the Time Complexity is O(n)O(n), where nn is the size of the string.

While an auxiliary space is required to store the string in a reverse manner, the Space Complexity is O(n)O(n), where nn is the size of the string.

Method - 3: Without Using Any Extra Space

We will discuss an optimal approach to reverse words in a string Python. The intuition of this approach is to swap the words of the string from the beginning and end, using a two-pointer approach, to reverse the string in constant space.

The algorithm is demonstrated as follows :

  1. Convert the string into an array of strings by splitting it on space.
  2. Initialize the two pointers left and right to 0 and len(string) – 1, respectively.
  3. While the left pointer does not exceed the right pointer, swap the elements at the left and right pointers, and move the left pointer forward and the right pointer backward by 1 step.
  4. Finally, return the final reversed string.

Let us now see the implementation of this approach to reverse words in a string in Python :

Output:

Complexity analysis

Since we are traversing the whole string to split it in space, the Time Complexity is O(n)O(n), where nn is the size of the string.

While an auxiliary space is required to store the string in a reverse manner, the Space Complexity is O(1)O(1).

Learn More:

To study the split function in Python, please refer here.

To learn more about the join function in Python, check this article.

Check out this article to learn more about Reverse a Number in Python.

To learn more about reverse string in python, click here.

To learn more about reversed in python, click here.

Conclusion

  • In this article, we have used Python built-in string functions like split(), reversed(), and join() in our program to reverse words in a string.
  • The Optimal Approach for the program has the Time Complexity O(n)O(n) and Space Complexity O(1)O(1) .