How to Sort the Letters in a String Alphabetically in Python

How to sort the letters in a string alphabetically in Python

You can do:

>>> a = 'ZENOVW'
>>> ''.join(sorted(a))

Python: How to sort the letters in a string alphabetically keeping distinction between uppercases and lowercases

sorted() sorts based off of the ordinal of each character. Capital letters have ordinals that are lower than all lowercase letters. If you want different behavior, you'll need to define your own key:

c = sorted(s, key=lambda c: (c.lower(), c.islower()))

That way, c would be sorted by ('c', 1) and C is sorted by ('c', 0). Both come before ('d', ...) or ('e', ...) etc., but the capital C is earlier (lower) than the lowercase c.

By the way, you shouldn't say d = "".join(sorted(c)) because c has already been sorted. Just do d = "".join(c)

Sorting characters of a string in Python [closed]

Your solution is correct as strings are immutable in python. So, it's impossible to change (in your case - sort) an existing string. You have to create new one (you do it with join() call).

Also, good notes about sorting letters in string in python can be found here: How to sort the letters in a string alphabetically in Python

How to sort alphabetically without repetitious letters in python? [closed]

from collections import Counter

def layered_sort(s):
if not s: return s
c = Counter(s)
keys = sorted(c)
max_repeats = max(c.values())
return "".join([k for i in range(max_repeats) for k in keys if c[k] > i])

test_cases = (
"the rain in spain falls mainly on the plain"

for s in test_cases:
print(repr(s), "sorts to", repr(layered_sort(s)))


'aaccbb' sorts to 'abcabc'
'aacb' sorts to 'abca'
'aaab' sorts to 'abaa'
'the rain in spain falls mainly on the plain' sorts to ' aefhilmnoprsty aehilnpst ailn ailn ain n '

How to properly sort characters alphabetically with mixed case?

Use a tuple inside your key function:

sorted_list = sorted(word ,key = lambda s: (s.upper(),s ))

The second param of the tuple will come in hand when the first param is identical: this will sort by uppercases letters first (sorting 'D'and 'd' the same), then sort by the letter ('D'<'d' - hence D comes first)

and you get:


str.capitalize is not needed, you are dealing with single letters anyway - capitalize is good for words to Start Wth A Capital Letter - you can use upper here.

How to sort list of strings alphabetically with upper and lower values?

Okay, so the sort function allows you to specify a key with which to compare elements;



Trying to sort two combined strings alphabetically without duplicates

You have two options here. The first is the answer you want and the second is an alternative method

To filter out duplicates, you can make a blank string, and then go through the returned string. For each character, if the character is already in the string, move onto the next otherwise add it

out = ""
for i in returned_string:
if i not in out:
out += i
return out

This would be empedded inside a function

The second option you have is to use Pythons sets. For what you want to do you can consider them as lists with no dulicate elements in them. You could simplify your function to

def longest(a: str, b: str):
return "".join(set(a).union(set(b)))

This makes a set from all the characters in a, and then another one with all the characters in b. It then "joins" them together (union) and you get another set. You can them join all the characters together in this final set to get your string. Hope this helps

Related Topics

Leave a reply