Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Sorting Strings With sorted()

00:00 Sorting strings is pretty similar to how you were sorting numbers before. Go ahead and open up your Python interpreter, and make a new string containing a couple of numbers.

00:10 So, something like string_number_value, and set this equal to '34521'. And while you’re here, go ahead and make a more traditional string that’s going to contain just a sentence, like 'I like to sort'. Like before, save some sorted versions of the strings, so sorted_string_number is just going to equal sorted(), and then pass in string_number_value.

00:41 And for your regular string, just call that sorted_string, and set that equal to sorted(), and then just pass in string_value.

00:50 All right! Let’s take a look at what we’ve got. The sorted_string_number looks like what you might expect. sorted() returned a list containing each of the numbers, and they’re in ascending order.

01:03 You should note that sorted() went through the string character by character, so it iterated over it just like a list. A good way to see this is to take a look at the sorted_string that contains the sentence, because here you can see that you have spaces (' ') that were sorted, and then you have each letter individually.

01:23 If you want to sort the words in a sentence, you’re going to need to break that string up into a list of words. You can do that with a .split() method.

01:31 Go ahead and make a new string called sorted_string_words, and in here you’re going to call sorted() and now pass in the string_value, but call .split() on there,

01:46 keeping in mind that with nothing passed into .split(), it’s going to split the string at the spaces. Now if you take a look at sorted_string_words,

01:55 you can see that each word was separated out and put in alphabetical order. If you wanted to bring these back together, you could use .join().

02:04 So just put a space inside some quotes and then .join(sorted_string_words).

02:12 This will just take each element in that list and join them with a space in between. And that’s about all there is to sorted() using the default parameters.

02:22 You now know that you can pass in lists, sets, tuples, or strings, and sorted() will go ahead and sort those based on if they’re numbers or letters—or even words! You may have noticed up here that this 'I' was put before 'e', even though i comes after e in the alphabet. We’ll get into this later, when we talk about some of the common errors that you can run into with sorting.

02:46 But in the next couple of videos, you’re going to see how you can change the default behavior of sorted() to customize it to your specific use case. Thanks for watching.

Avatar image for moriar77

moriar77 on May 3, 2021

Thank you

Become a Member to join the conversation.