x = 'yellow'
print(sorted(x))
returns
['e', 'l', 'l', 'o', 'w', 'y']
What I want it to return
ellowy
How ran I make it return 'ellowy'
without the letters being in a list?
Actually sorted()
when used on a string always returns a list of individual characters.
so you should use str.join()
to make a string out of that list.
>>> x = 'yellow'
>>> ''.join(sorted(x))
'ellowy'
The join()
method of a string joins each element of its argument with copies of the given string object as a delimiter between each item. Many people find this a weird and counterintuitive way to do things, but by joining the elements with an empty string you can convert a list of strings into a single string:
x = 'yellow'
print(''.join(sorted(x)))
x = 'yellow'
print(''.join(sorted(x)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With