• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Reverse Words in a String Python

Reverse Words in a String Python

February 22, 2022 Leave a Comment

In Python, we can easily reverse words in a string in Python using the Python split(), reverse() and join() functions.

def reverseWords(string):
    words = string.split()
    words.reverse()
    return " ".join(words)

print(reverseWords("this is a string with words"))

#Output:
words with string a is this

You can also use the split() function, slicing, and the join() function to reverse the words in a string with Python.

def reverseWords(string):
    words = string.split()
    return " ".join(words[::-1])

print(reverseWords("this is a string with words"))

#Output:
words with string a is this

When using string variables in Python, we can easily perform string manipulation to change the values or order of the characters in our string.

One such manipulation is to reverse the words in a string.

To reverse the words in a string, we can use the split() function to get a list of each word in the string, and then reverse the items in the list.

After reversing the list with the reverse() function or slicing, we then join the words together with join().

Below is an example function of how to reverse the words in a string using Python.

def reverseWords(string):
    words = string.split()
    words.reverse()
    return " ".join(words)

print(reverseWords("this is a string with words"))

#Output:
words with string a is this

As mentioned above, you can also use slicing to reverse the list of words.

def reverseWords(string):
    words = string.split()
    return " ".join(words[::-1])

print(reverseWords("this is a string with words"))

#Output:
words with string a is this

Reversing Each Word in a String Using Python

If you are looking to reverse each word in a string, we can modify our examples from above slightly. Instead of reversing the order of the words, we will reverse the letters of each word.

In this case, we will split() the string to get each word, and then loop over each word and reverse it with string slicing.

After the loop is done, we will join the words back together.

Below is an example of how to reverse each word in a string with Python.

def reverseWords(string):
    words = string.split()
    for i in range(0,len(words)):
        words[i] = words[i][::-1]
    return " ".join(words)

print(reverseWords("this is a string with words"))

#Output:
siht si a gnirts htiw sdrow

Hopefully this article has been helpful for you to learn how to reverse the words in a string using Python.

Other Articles You'll Also Like:

  • 1.  How to Iterate over Everything in Word Document using python-docx
  • 2.  PROC MEANS Equivalent in Python
  • 3.  Fibonacci Sequence in Python with for Loop
  • 4.  Selenium minimize_window() Function to Minimize Window in Python
  • 5.  Create Empty Tuple in Python
  • 6.  Python Indicator Function – Apply Indicator Function to List of Numbers
  • 7.  Perfect Numbers in Python
  • 8.  Divide Each Element in List by Scalar Value with Python
  • 9.  Python Turtle Fonts – How to Write Text with Different Fonts in Python
  • 10.  Create List of Odd Numbers in Range with Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy