Learning About the maxsplit Argument
00:00 So far, you’ve been splitting your strings into as many pieces as possible, but sometimes that’s not what you want. What if you only need to split off the very first part of the string and keep the rest of it together?
00:12
Or maybe you want to separate a key from a value in a key-value pair. In such scenarios, you can control the number of splits by using the maxsplit
argument.
00:23
Let’s see some examples. In this example, if you only want to split the first word, there is one. You can set the maxsplit
to one. When you set the maxsplit
to one, the .split()
method finds the first separator.
00:36
In this case, a whitespace character, performs a split, and then stops. When you use the maxsplit
argument, the list you get back will always have max
split + 1
items.
00:48
In this case, that is two items. If you want to split twice, just set the maxsplit
to two. This time it splits at the first space, and then at the second space, and then it stops.
01:00
The rest
variable now contains the entire remainder of the string that is “three four” without any further splits.
01:09
Now that you know how to control the number of splits the .split()
method makes, you might be wondering, when am I ever going to use this? So let’s see some practical examples of using the maxsplit
argument, and let’s try some code too.
01:23
So the first use case where maxsplit
is very useful is in command-line arguments. Let’s see it in action. Let’s say you have an application that sends email to any user, and you have user input that looks like this.
01:46
The user input has three components. The first word that is the command that is “send” in this case, and then the user email, and finally the message. Now, if you use the regular .split()
method, our message that is, “Hello, how are you,” will also be split into different parts.
02:01
So what we can do is we can use the maxsplit
to control the number of splits our .split()
method makes.
02:16
So we have told the .split()
method to make just two splits. That is the first word and the second word. So let’s see what it has returned.
02:34 Now we have the first and the second word that is the command, and then the user email, and let’s see if our message is also intact.
02:47 So now the integrity of our message is also intact and it is not split into different words.
02:55
The maxsplit
argument can be very helpful when we are handling structured data too. For example, key-value pairs or log lines. If our key-value pair is separated by an equals sign, we can just set the delimiter to =
two and set the maxsplit
argument to one.
03:11
When we do that, we make sure that the value remains intact, even if there’s another equals sign in the value. To extract date, log level, and log message from a log line, what we did last time, if you remember, we split using the default .split()
method, and then we had to join all the words of the message later on as they were also split by this default .split()
method.
03:36
Using this maxsplit
, what we can do is we can set maxsplit
to two so that our list will have three items. The first item will be date, then we will have the log level, and finally we will have the log message that will be intact.
03:50 So our log entry will just have made two splits, one on the first whitespace character, and second after the second whitespace character. This way, we can get the whole message intact and we won’t have to join the message later on.
04:05
Now let’s see the maxsplit
argument in action. Now, this is the previous example where you had to join all the words of the message because all the words had also been split since you were using the default .split()
method.
04:18
Let’s see how this has changed using the maxsplit
argument.
04:26
Now you want to split on the first whitespace character and on the second whitespace character too. So you want to set the maxsplit
to two. Let’s see what our parts
variable holds.
04:41 So we get the date and the error because it was split on the first whitespace character. And after splitting on the second whitespace character, we get the full error message.
04:51
If you just want the error message, you can just print()
04:58 the element in the second index and you get your full error message.
Become a Member to join the conversation.