Loading video player…

Using the .split() Method: Fundamentals

00:00 You’ve had a quick refresher on strings. They’re immutable sequences. Now let’s get into how you can actually use the .split() method. You’ll start with the basic syntax, how you can call it, and the arguments you can give it to fine-tune its behavior.

00:14 Then you’ll see how it works out of the box with its default settings. Finally, you’ll see some common situations where you’d use this default setting.

00:24 You call the .split() method on a string object that you want to break apart. Let’s understand the full syntax. First, there is simply the string you want to split.

00:32 Then comes the method. The .split() method takes two arguments. separator is the first argument and it’s optional. The separator is a specific string that the .split() method will search for to decide where to split.

00:45 So it’s your delimiter. If you don’t provide anything for separator or if you explicitly pass None, which is the default, .split() splits on whitespace characters, multiple spaces, tabs, or new lines.

00:59 You’ll see this in action very soon. maxsplit is the second optional argument. maxsplit lets you control the maximum number of splits that will actually be performed on your string.

01:10 If you set maxsplit to one, the .split() method will find the first place the separator occurs, make one split there, and then stop.

01:18 The list you get back will have two items, the part of the string before the separator and the rest of the string after it. If you set maxsplit to two, it’ll split at the first two occurrences and you’ll get a list with three items.

01:31 The default value for maxsplit is -1. This is a special value that tells the .split() method to make all possible splits by going through the entire string.

Become a Member to join the conversation.