+ 2

How to print sorted string in python ?

Like Sample I/p: I love to code Sample o/p: code I love to

19th Apr 2020, 7:36 PM
sai kumar
sai kumar - avatar
56 Réponses
+ 5
def fun(x): return x.lower() a = input().split() a.sort(key = fun) ans = ' '.join(a) print(ans)
22nd Apr 2020, 2:49 PM
Moe Kyaw Oo
Moe Kyaw Oo - avatar
+ 3
My Python 3 solution:- n = input().split() Dict, li = {}, [] for i in n: Dict[i.lower()] = i li.append(i.lower()) li.sort() for i in li: print(Dict[i], end = " ") I am new to Python. This will surely work sai kumar . Please like this comment if you found this helpful.
21st Apr 2020, 11:51 AM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 2
It works... But maybe the prob is that not all words are lowercase I love to code // i love to code
19th Apr 2020, 7:52 PM
Oma Falk
Oma Falk - avatar
+ 2
Better still, post your code so we can know how to really help you out.
19th Apr 2020, 7:55 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 2
1)msg=str(input()).split() 2)msg.sort() 3)print(' '.join(msg))
20th Apr 2020, 1:53 PM
Bai Jilla
Bai Jilla - avatar
+ 2
.strip() remove characters from both left and right based on the argument..
21st Apr 2020, 2:40 PM
vishwateja.Gorumanthula
vishwateja.Gorumanthula - avatar
+ 2
Mr.Gaurav urs is also correct, there r multiple ways in answering a problem
21st Apr 2020, 2:55 PM
sai kumar
sai kumar - avatar
21st Apr 2020, 2:55 PM
sai kumar
sai kumar - avatar
+ 2
This is good. Moe Kyaw Oo. Using a function as the sort key....OMG. That is neat!
22nd Apr 2020, 5:06 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 1
Neglect the upper and lower case in this,
19th Apr 2020, 7:54 PM
sai kumar
sai kumar - avatar
+ 1
No
19th Apr 2020, 8:33 PM
sai kumar
sai kumar - avatar
+ 1
n = input() l = sorted(n.split(), key=lambda x: x.lower()) print(l[0], l[-1]) # or mystring = l[0] + ' ' + l[-1] print(mystring)
19th Apr 2020, 8:57 PM
rodwynnejones
rodwynnejones - avatar
+ 1
I always go to youtube, geeksforgeeks or stackoverflow then see if my question has already been solved. I found a youtube video for you because I think it would benefit you if you know how strings work in general in python. https://youtu.be/UsCQXe1OHZk
20th Apr 2020, 6:05 AM
DeJon
+ 1
No use, just see my code. It's perfectly working Ramya Thiurmalisamy
21st Apr 2020, 2:37 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 1
But i dont think here .strip() is needed. Just check out my code sai kumar
21st Apr 2020, 2:42 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 1
I think question is self explanatory. You have given a string and you have to sort the string on the basis of it's words i.e Lexicographically e.g (1) Input - are you ok Output - are ok you e.g (2) Input - This is important Output - important is This
21st Apr 2020, 3:48 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 1
Tomiwa Joseph bro you will never get what I want to say. Just go through my code and see what the outcome is when you input "This is important" is returned as "important is This"
21st Apr 2020, 8:13 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
+ 1
This will surely work Moe Kyaw Oo
22nd Apr 2020, 3:20 PM
Gaurav Kumar Prabhat
Gaurav Kumar Prabhat - avatar
0
What have you tried already my man?
19th Apr 2020, 7:39 PM
Slick
Slick - avatar
0
I tried the following,but not passed,i think it's wrong,I searched the solution but not found the clear one n=input() k=sorted(n.split()) l=' '.join(k) Print(l)
19th Apr 2020, 7:44 PM
sai kumar
sai kumar - avatar