Sort words in lexicographic order in Python

| | | |


 Input: "hello python program how are you" Output: are hello how program python you Input: "Coders loves the algorithms" Output: Coders algorithms loves the 

Note: words that have the first letter are capital letters and will be printed in alphabetical order.

The approach used in this program is very simple. Split the lines using the split () function. After that, sort the words in lexicographic order using sort (). Repeat words in a loop and print each word that is already sorted.

# Python program for sorting words in lexicographic
# order

def sortLexo (my_string):

# Split string my_string until space is found.

words = my_string.split ()

# sort () will sort the lines.

words.sort ()

# I iterate through "words" to print words

# in alphabetical order.

for i in words:

print (i)

# Driver code

if __ name__ = = ’ __main__’ :

my_string = " hello this is example how to sort "

"the word in alphabetical manner"

# Function call

sortLexo (my_string)


 alphabetical example hello how in is manner sort the this to word 

Sort words in lexicographic order in Python lexicographic order: Questions

Sort words in lexicographic order in Python Python functions: Questions


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method