I understand there is sort() function, but it won't work for me here. I would like to order alphabetically a string like follows:
'S NOM V NOUN VERB'
'NOM NOUN S VERB V'
Try the following:
x = 'S NOM V NOUN VERB' x = x.split() # produces ['S', 'NOM', 'V', 'NOUN', 'VERB'] x = sorted(x) # produces ['NOM', 'NOUN', 'S', 'V', 'VERB'] x = ' '.join(x) # produces 'NOM NOUN S V VERB'
You will have to use a custom sorting function if you want the order of V and VERB to be reversed (see the 'key' keyword for the