Autumn Offers - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Searching
Sorting – sort()
Sorting – sorted()
11. sorted() with other data structures
Sorting – reverse() and reversed()
Summary

Instruction

Good job! Another difference between sort() and sorted() is that the latter accepts any kind of data structure you can iterate over. No matter the input data structure, however, the output will always be a list. Take a look:

chaotic_string = 'fjzssdas'
sorted_string = sorted(chaotic_string)
print(sorted_string)

Result:

['a', 'd', 'f', 'j', 's', 's', 's', 'z']

Even though sorted() accepts a string argument, it outputs a list of sorted characters, not a sorted string.

Exercise

Write a function named sort_string(input_string) that accepts a string and returns a new string:

  1. The new string should have all the characters from the input string, but they should all be changed to lowercase.
  2. The characters in the new string should be sorted alphabetically.

Stuck? Here's a hint!

Use sorted(), but remember that it returns a sorted list, not a string. Think about a way of changing a list of characters to a string.

To change a string to lowercase, use:

string.lower()

To create a string from list of letters, use:

''.join(list)