Exploring Common Use Cases
00:00
Now let’s go ahead and look at some common use cases. A basic and classic use case is text processing. Calling the .split()
method on a sentence will give you a list containing each of the words in the sentence.
00:12
Let’s look at an example. Say you have a variable called description
and it contains the string, "Python is a versatile programming language"
.
00:26
The list of words is often called tokens in text analysis. So let’s tokenize this sentence by calling the .split()
method.
00:49 The list of words is now ready for you to work with. Maybe you want to count how often each one appears or check if certain keywords are present or not. Once you have your tokens ready, you can just go ahead and code the rest of your application logic.
01:05
Other common use cases: data cleaning. Let’s say user input has extra spaces. The .split()
method handles this for you without any fuss. Let’s see this in action.
01:16 Let’s say you have a user command in a sentence.
01:24 There can be some additional whitespace in the command. And for this application logic, let’s assume you want to check the first word of the sentence, and if the first word is “show”, you’ll go ahead and show some options.
01:36 Now you can split the sentence.
01:45 Now check if the word list is empty or not. And if the word list is not empty, check the first word of the list.
01:58
If the first word is “show”, you can go ahead and write the rest of the application logic here. You can just print "Show options"
for now. And in this example, you’ve taken a sentence.
02:13
You have split the sentence into words and removed all the white spaces, and you’ve checked if the word list’s first word is "show"
. And if it is "show"
, you’ve just gone ahead with the rest of the application logic.
02:26
Keep in mind that the .split()
method will remove all the leading whitespaces and all the trailing white whitespace from the sentence. So the first element in the command_components
list will be the word "show"
.
02:40
If you have simple text files where data items on a line are separated by spaces, the default form of the .split()
method can be very effective for reading each line and extracting required information from the line or the file. Log files are a common example.
02:56 Let’s take a look. Let’s say a log line in your log file contains date, log level, and the log message. In this example, let’s say the log level is “ERROR” and the log message is “Disk Full”.
03:11
And you want to separate log date, log message, and the log level. So you can just split the log_line
.
03:20
Now let’s check the parts
list. And here you have your date, you have the log level, and then you have the message. The problem here is since messages can be a long sentence, that message sentence will also be divided into different words that are part of the parts
list.
03:40
So what you can do is you can assign each part into an appropriate variable. So we have date
, level
, and the rest of the list. This asterisk in front of message
is called extended iterable unpacking. The asterisk message
collects all remaining items from the iterable parts that haven’t been assigned to date
and level
.
04:03
In this version, you manually join the last two parts to get the full error message that is 'Disk'
'Full'
. It works, but there’s actually a cleaner way to do this using the maxsplit
argument. It lets you control how many times the string gets split, which can make your code more elegant and easier to read.
04:20
We will revisit this exact example later in the course when we dive into how maxsplit
works. So let’s print this, and now you can use your date.
04:31 You can use the log level,
04:37
and if we just print a message, it’ll be a list. Now a nifty trick is to join these two to create a sentence. So for that, you can use the join()
method of the string and pass the message list.
04:56 And now you have your error message.
05:01
Now let’s see how we can use the .split()
method for string manipulation. Let’s say you have a title that is "welcome to the jungle"
, and you want to convert this to title case, that is the first character of each word needs to be capital.
05:21 So the first step will be to split the sentence. Now, you will loop through each word and capitalize the first letter of each word, and after it has been capitalized, you’ll want to join each word to create a new sentence again.
05:37
So like the last time, you can use the .join()
method of string. Capitalize the first word for each word in the word’s list. Now print your new sentence, and now you have successfully converted your title to title case.
05:58
Now that you’ve seen some common use cases of the .split()
method and how to split strings based on the whitespace character, let’s see how you can split a string based on something other than the whitespace character.
Become a Member to join the conversation.