Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want to sort every words in the sentence like this.

Enter the phrase: i love programming
Output: i elov aggimmnoprr

What I have tried:

word = str(input("Enter the phrase: "))
print(''.join(sorted(word)))
Posted
Updated 19-Oct-21 8:34am

 
Share this answer
 
Comments
Marvin Añasco 21-Oct-21 2:15am    
ty man?
Maciej Los 21-Oct-21 3:26am    
If "ty man" means: "Thank you, man" then: you're very welcome.
First, break the sentence into words: find all the separators (comma, full stop, space, and so forth) and use them to extract each word. Hint (if allowed by your teacher): what does the split function do?

Then, sort the collection of words. Hint (if allowed by your teacher): what does the sort function do?

Then join them back together with spaces, and print them.

But this is homework, so you'll get no code!
 
Share this answer
 
Comments
Marvin Añasco 19-Oct-21 5:27am    
ty man but i'm done with this
Nelek 19-Oct-21 9:54am    
Does it mean that you managed to solve it in 15 minutes?
If yes... why don't you try it on your own before posting a question online?
Marvin Añasco 21-Oct-21 2:14am    
ur too late, i'll already have code with this, but my problem is code, u can see my code above.
Marvin Añasco 19-Oct-21 5:30am    
i split the words by loop then after separate the words i use sorted() function for every words after that i use (end=' ') to print the same line

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900