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.

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 :

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:

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.

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) .