How To Reverse A Sentence In Python

This tutorial will teach you how to reverse a sentence in Python. If you are given a Sentence as input in Python and want to reverse that sentence, use the examples below.

Reversing a Sentence In Python, use the reversed() function with an input argument that contains the list of words from the sentence you want to reverse. The split() function can be used to generate a list of words from a sentence. Once the list has been reversed, use the join() function to create a string of the reversed list of words to obtain the reversed sentence.

Let us see in the below examples how you can reverse a given string of words using Python in different methods.

1. Using The reversed(), split(), and join() functions

To begin, use the split() function to divide the sentence into a list of words. Once you have a list of words, you can use the reversed() function to reverse the list. The sentence can then be created by using the join() function.

Let’s look at how to reverse a sentence in Python using the split(), reversed(), and join() functions in the code below.

#Initializing the String To Reverse
inputString = "Need To Reverse This String"

#Splitting The Input String To Words
wordList = inputString.split()

#Using reversed to reverse wordList
reversed(wordList)

#Using the join method to join the words
reversedString = " ".join(wordList)

#Printing the Reversed Sentence
print(reversedString)

Output:

String This Reverse To Need

2. Using The split(), Slicing, and join() Method

Using the split() function, first divide the sentence into a list of words, which will be used later. You can use the list slicing method to reverse the order of words in a list once you’ve created one for yourself. The join() function can then be used to construct the sentence that follows.

#Initializing the String To Reverse
inputString = "Need To Reverse This String"

#Splitting The Input String To Words
wordList = inputString.split()

#Using slicing to reverse wordList
wordListRev = wordList[::-1]

#Using the join method to join the words
reversedString = " ".join(wordListRev)

#Printing the Reversed Sentence
print(reversedString)

Output:

String This Reverse To Need

3. Using join(), split(), and reversed() Method in Optimized Way

You can use the above code and optimize it to perform the reversing of the sentence using only one line of code in the print statement. Or using a string variable to store the reversed string. Let us see in the below example code.

#Initializing the String To Reverse
inputString = "Need To Reverse This String"

#Opitimizing
revString = " ".join(reversed(inputString.split()))

#Printing the Reversed Sentence
print(revString)

Output:

String This Reverse To Need
Reverse A Sentence In Python

Wrap Up

Using the methods described above, I hope you were able to learn how to reverse a string and how to reverse a sentence in Python. I’ve listed three methods that you can use in your code to reverse any string.

If you know of a better method than the one discussed above, please let me know in the comments section and I will gladly add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Reverse A List In Python Using For Loop
  2. 3 Ways To Check If An Array Is Empty In Python
  3. How To Fix The Package-lock.json File Was Created With Old NPM Version
  4. How To Create A Dictionary In Python
  5. How To Get First Key From Dictionary Python – 3 Methods

Leave a Comment