0
0
PythonProgramBeginner · 2 min read

Python Program to Reverse Words in String

You can reverse words in a string in Python by splitting the string with split(), reversing the list with [::-1], and joining it back with join(), like this: ' '.join(input_string.split()[::-1]).
📋

Examples

Inputhello world
Outputworld hello
InputPython is fun
Outputfun is Python
Input
Output
🧠

How to Think About It

To reverse words in a string, first separate the string into individual words by splitting at spaces. Then reverse the order of these words. Finally, join the reversed words back into a single string with spaces between them.
📐

Algorithm

1
Get the input string.
2
Split the string into a list of words using spaces.
3
Reverse the order of the words in the list.
4
Join the reversed words back into a string with spaces.
5
Return or print the reversed string.
💻

Code

python
def reverse_words(s):
    words = s.split()
    reversed_words = words[::-1]
    return ' '.join(reversed_words)

input_string = "Python is fun"
print(reverse_words(input_string))
Output
fun is Python
🔍

Dry Run

Let's trace the input 'Python is fun' through the code

1

Split the string

words = ['Python', 'is', 'fun']

2

Reverse the list

reversed_words = ['fun', 'is', 'Python']

3

Join reversed words

'fun is Python'

StepWords List
After split['Python', 'is', 'fun']
After reverse['fun', 'is', 'Python']
After join'fun is Python'
💡

Why This Works

Step 1: Splitting the string

Using split() breaks the string into words by spaces, making it easier to work with each word separately.

Step 2: Reversing the list

Using [::-1] reverses the order of words in the list, so the last word comes first.

Step 3: Joining words back

Using join() combines the reversed words into a single string with spaces between them.

🔄

Alternative Approaches

Using reversed() function
python
def reverse_words(s):
    words = s.split()
    reversed_words = reversed(words)
    return ' '.join(reversed_words)

print(reverse_words('Python is fun'))
This uses the built-in <code>reversed()</code> function which returns an iterator; it is readable and efficient.
Using a loop to build reversed string
python
def reverse_words(s):
    words = s.split()
    reversed_words = []
    for i in range(len(words)-1, -1, -1):
        reversed_words.append(words[i])
    return ' '.join(reversed_words)

print(reverse_words('Python is fun'))
This manually reverses the list using a loop; it is more verbose but shows the reversal process clearly.

Complexity: O(n) time, O(n) space

Time Complexity

Splitting the string and joining words each take O(n) time where n is the length of the string. Reversing the list is also O(n). Overall, the time is O(n).

Space Complexity

Extra space is needed to store the list of words and the reversed list, so space complexity is O(n).

Which Approach is Fastest?

Using slicing [::-1] or reversed() are both efficient and readable. Manual loops are slower and more verbose.

ApproachTimeSpaceBest For
Slicing with [::-1]O(n)O(n)Simple and fast for reversing lists
Using reversed()O(n)O(n)Readable and efficient iterator-based reversal
Manual loopO(n)O(n)Educational, shows reversal process explicitly
💡
Use split() and join() together with slicing to reverse words easily.
⚠️
Forgetting to split the string into words before reversing causes incorrect results.