Python | Sort the words of the sentence in ascending order

Python Methods and Functions

Examples:

 Input: to learn programming refer pythonengineering Output: pythonengineering learn programming refer to Input: geeks for geeks Output: for geeks geeks 

We will use a built-in library function to sort the words of a sentence in ascending order. 
Prerequisites:
1. split ()
2. sort () in Python
3.

# Function for sorting words
# ascending order

def sortedSentence (Sentence):

 

# Split sentence into words

  words = Sentence.split ( "" )

 

# Word sorting

words.sort ()

 

# Create a new proposal from

  # appending sorted words

newSentence = "" . join (words)

 

# Return newSentence

return newSentence

 
Driver code

 

Sentence = " to learn programming refer pythonengineering "

# Print sortedSentence

print (sortedSentence (Sentence))

 

Sentence = "geeks for geeks"

# Print sortedSentence

print (sortedSentence (Sentence))

Output:

 pythonengineering learn programming refer to for geeks geeks 




Tutorials