1
00:00:04,680 --> 00:00:09,600
We've seen how to join items in an
iterable, using the join method.

2
00:00:09,600 --> 00:00:15,140
Strings have an opposite method - the
split method. We called our last Python

3
00:00:15,150 --> 00:00:18,780
file joining_things, so let's
create a new one, and I'm going to call

4
00:00:18,780 --> 00:00:22,550
this one splitting_ things.

5
00:00:30,720 --> 00:00:34,700
The split method splits a string up into words.

6
00:00:34,700 --> 00:00:38,880
Now that sounds simple, so let's see it
working. So I'm gonna start by typing

7
00:00:38,880 --> 00:00:48,240
panagram is equal to, and in double
quotes, The quick brown fox jumps over

8
00:00:48,240 --> 00:00:59,640
the lazy dog. On line three, I'm going to
type words is equal to panagram.split,

9
00:00:59,640 --> 00:01:04,080
leaving the parentheses in so
we're calling the method. On line four I'm

10
00:01:04,080 --> 00:01:13,420
just going to print out words, so print,
words in parentheses. When we run that,

11
00:01:13,420 --> 00:01:18,780
we get a list containing all the words in
the string. We didn't provide any

12
00:01:18,790 --> 00:01:23,140
arguments to split, so it defaults to
splitting on whitespace. Whitespace

13
00:01:23,140 --> 00:01:27,040
includes things like tabs and newlines,
as well as a few other characters,

14
00:01:27,040 --> 00:01:31,660
including the space. So to demonstrate
that, what I'll do is use triple quotes

15
00:01:31,660 --> 00:01:35,540
and space the string over several lines.
Let's go ahead and do that.

16
00:01:35,540 --> 00:01:40,620
We've got three quotes at the start here, and
two down the end, and we're gonna space

17
00:01:40,630 --> 00:01:45,850
it over multiple lines, as I said. So I'm
going to press ENTER after brown and

18
00:01:45,850 --> 00:01:52,420
move the space. In between the word jumps
and over, I'm going to put a tab, \t,

19
00:01:52,420 --> 00:01:57,429
I'm gonna press ENTER after the word
over, so you can see we've spaced that, now,

20
00:01:57,429 --> 00:02:03,970
over three lines. So if I run that, and
you can see we get exactly the same

21
00:02:03,970 --> 00:02:09,960
output, a list containing all nine words
of the string. Split returns a list.

22
00:02:09,960 --> 00:02:14,140
You call it on a string and it returns all
the items in the string, that are

23
00:02:14,140 --> 00:02:19,660
separated by one or more whitespace
characters. That's the default behaviour,

24
00:02:19,660 --> 00:02:23,120
but it also has a couple of named
arguments.

25
00:02:23,120 --> 00:02:26,880
I won't call up the documentation in a
browser. You've got the page bookmarked

26
00:02:26,890 --> 00:02:31,360
and you can refer to that after this
video, if you wish. Sometimes, you'll just

27
00:02:31,360 --> 00:02:35,940
want a quick reminder of the parameters
that a function or method defines.

28
00:02:35,940 --> 00:02:40,440
The JetBrains IDEs - IntelliJ
and Pycharm - have a useful feature to let

29
00:02:40,450 --> 00:02:47,019
you do that. Holding down the control key,
and hover over the method. You can see

30
00:02:47,019 --> 00:02:51,220
I'm doing that for split, there. Now on a
Mac you'll use the command key, and also

31
00:02:51,220 --> 00:02:55,989
most modern IDs will also have a similar
feature. So as you can see, that's a

32
00:02:55,989 --> 00:03:00,459
useful way to get a quick reminder about
the arguments you can use, as well as the

33
00:03:00,460 --> 00:03:05,260
names of named arguments. The first
parameter you can see there is called self.

34
00:03:05,260 --> 00:03:08,620
Now this might sound strange, but
I'm going to ask you to pretend that

35
00:03:08,620 --> 00:03:11,799
isn't there for the moment. One
difference between a function and a

36
00:03:11,799 --> 00:03:16,510
method, is a presence of this self
parameter. You'll learn all about that in

37
00:03:16,510 --> 00:03:21,430
the Object-Oriented Programming section
of this course. When you're using the

38
00:03:21,430 --> 00:03:26,840
methods, Python provides a value for self
for you, so don't worry about it at the moment.

39
00:03:26,840 --> 00:03:31,030
So moving on next, we can see the
sep named argument. That's used to decide

40
00:03:31,030 --> 00:03:35,590
what separates the words in the string.
As I've said previously, the default is

41
00:03:35,590 --> 00:03:39,519
whitespace, but you may have a comma
separated list, for example, that you want

42
00:03:39,519 --> 00:03:45,040
to split. In that case, you can provide a
comma as the separator. If you recall,

43
00:03:45,040 --> 00:03:48,850
we saw an example of that way back in
section 4, when we extracted all the

44
00:03:48,850 --> 00:03:52,569
values from a string. So let's go through
that again. We had a string containing

45
00:03:52,569 --> 00:04:03,580
digits separated by commas. So I'm going
to type on line 8, numbers is equal to 9 comma

46
00:04:03,580 --> 00:04:16,260
223,372,O36,854,775 and
807. We can tell split to split the

47
00:04:16,269 --> 00:04:20,769
string up at the commas, to get a list of
just sequences of digits. So I could type

48
00:04:20,769 --> 00:04:25,030
print, in parentheses I can type numbers

49
00:04:25,030 --> 00:04:29,950
dot split parentheses and this time,
within the parentheses, I'm going to put

50
00:04:29,950 --> 00:04:37,990
a comma in double quotes. So run the
program - you can see, we've got a list

51
00:04:37,990 --> 00:04:42,610
there, containing each of the numbers
from our number string. Now note that

52
00:04:42,610 --> 00:04:47,560
they're all strings. When you split a
string, you get a list of strings. That's

53
00:04:47,560 --> 00:04:51,639
why I had to use that strange code, back
in section 4. I wanted to add up all the

54
00:04:51,639 --> 00:04:56,470
values, so they had to be converted to
integers first. You'll understand what

55
00:04:56,470 --> 00:05:00,560
that code was doing, later in the course.
It's basically, a more concise way

56
00:05:00,560 --> 00:05:05,300
of writing the code that you will create
for the challenge, at the end of this video.

57
00:05:05,300 --> 00:05:08,400
Now there was another strange bit
of code in that example. I'm going to

58
00:05:08,410 --> 00:05:13,930
paste that back in here, so we can talk
about it. Now don't bother typing that

59
00:05:13,930 --> 00:05:17,140
code in because I'm not going to run it.
I just want to show you how we used

60
00:05:17,140 --> 00:05:21,250
split in that earlier example. It won't
run in any case, at the moment. We've got

61
00:05:21,250 --> 00:05:25,510
an error because separators isn't
defined. You should have the complete

62
00:05:25,510 --> 00:05:28,440
code in the strings2.py file from the last section,

63
00:05:28,440 --> 00:05:32,580
if you want to refer back to it. But
looking at the code on line 11, we used

64
00:05:32,590 --> 00:05:37,180
the join and split commands. The line
still won't make complete sense because

65
00:05:37,180 --> 00:05:41,800
of that strange code that we're passing
to join. What I'll do, though, is replace

66
00:05:41,800 --> 00:05:47,080
the code with the sequence that it
produces. Okay, so the strange code on

67
00:05:47,080 --> 00:05:52,690
line 11, produced the generated list on
lines 12 to 18, so it, basically, replaced

68
00:05:52,690 --> 00:05:58,000
all the separators with spaces and
produce the list of all the characters.

69
00:05:58,000 --> 00:06:01,780
And you'll learn how that worked later.
We can now start, now, to make sense of

70
00:06:01,780 --> 00:06:07,540
how join and split were being used
together. Let's focus on the join part first,

71
00:06:07,540 --> 00:06:12,100
and what I'll do is, I'll just
comment out line 11 now. Alright, so

72
00:06:12,100 --> 00:06:19,000
let's focus on the join part of line 19,
first. First we're gonna start by deleting

73
00:06:19,000 --> 00:06:23,320
the dot split on the end of that line,
like so, and we'll add it back shortly.

74
00:06:23,320 --> 00:06:29,580
First though, let's see what join produces,
so let's run on the program. We've called

75
00:06:29,590 --> 00:06:35,800
join on an empty string on line 19. That
means the items in the list won't be

76
00:06:35,800 --> 00:06:40,320
separated from each other by anything.
Remember that the string you use

77
00:06:40,320 --> 00:06:45,300
when calling join, is used to separate the
items that you're joining. So we end up

78
00:06:45,300 --> 00:06:50,060
with a string containing all the items
from generated list,

79
00:06:50,060 --> 00:06:55,040
essentially, from our generated list there,
on line 12. All the items were single

80
00:06:55,050 --> 00:06:58,229
characters, and that's what we've got in
our output - a string containing the

81
00:06:58,229 --> 00:07:02,669
single characters from the list. Each
sequence of digits was followed by a

82
00:07:02,669 --> 00:07:07,289
space, and we get those spaces
in our output. And again, I'll just run it

83
00:07:07,289 --> 00:07:11,879
again so we can see it. Alright, so the next
thing we want to do is split that string

84
00:07:11,879 --> 00:07:16,319
up again. We know that split will split
on whitespace, and we can see the spaces

85
00:07:16,320 --> 00:07:23,000
in the string, on the last line of our
output. So let's add some code, so down here,

86
00:07:23,000 --> 00:07:31,180
on line 22, I'm going to type values_list is equal to values.split(),

87
00:07:31,180 --> 00:07:37,680
so I'm calling the split function. Then
we'll print out the value, or values_list,

88
00:07:37,680 --> 00:07:41,520
or the contents, I should
say, our values_list on the next line.

89
00:07:41,520 --> 00:07:45,120
So we haven't provided a
separator when we call .split, so it

90
00:07:45,120 --> 00:07:50,060
splits up the string at the spaces, and
if we run this,

91
00:07:52,400 --> 00:07:56,570
we can see that the last line of the
output is a list, containing each of the

92
00:07:56,570 --> 00:08:01,100
numbers that appeared in the string. And
we can see the string, on the line above that,

93
00:08:01,100 --> 00:08:05,840
in the output. Alright, so that's
the join and split methods; join will let

94
00:08:05,840 --> 00:08:10,130
you create a string from a sequence, such
as a list split. Split, on the other hand, will

95
00:08:10,130 --> 00:08:14,449
split the string up again and create a
list. We'll be using these methods again,

96
00:08:14,449 --> 00:08:18,710
in some of the later examples. Split can
be very useful when reading data from a

97
00:08:18,710 --> 00:08:23,479
file, for example. Experiment with your
own lists and strings, and make sure you

98
00:08:23,480 --> 00:08:28,640
understand how to use these two methods
before continuing. For now, though,

99
00:08:28,640 --> 00:08:32,679
let's finish this video with a mini-challenge. So
looking at that last line of output,

100
00:08:32,679 --> 00:08:37,400
we've got a list of strings. Back in
section 4, I used a comprehension

101
00:08:37,400 --> 00:08:42,340
to convert that to a list of numbers. We
haven't covered comprehensions yet, but

102
00:08:42,340 --> 00:08:46,780
we have covered everything you need, to
produce a list of ints. So your challenge is

103
00:08:46,780 --> 00:08:51,920
to use a for loop, to replace all the
strings in that list with integer values.

104
00:08:51,920 --> 00:08:54,680
So what I'm going to do is close down
the run window. So I'm going to paste in

105
00:08:54,680 --> 00:08:59,350
the comments now - the challenge in a
series of comments, as you can see there.

106
00:08:59,350 --> 00:09:03,700
So that's the challenge. See how you go
with it, and I'll go over my solutions to

107
00:09:03,700 --> 00:09:07,500
that challenge in the next video.

