1
00:00:04,560 --> 00:00:09,840
Strings have a join method that's very
useful for producing output from a list,

2
00:00:09,840 --> 00:00:13,280
and in fact, it can be used with any
iterable. What I'm going to do is use the

3
00:00:13,280 --> 00:00:19,550
first for loop in our nospam program, for
this example. What I'm going to do is

4
00:00:19,550 --> 00:00:28,640
uncomment the first loop, then I'm going
to delete the rest of the code below that.

5
00:00:28,640 --> 00:00:31,300
Alright, now I've done that, I'm
going to run the program to remind

6
00:00:31,300 --> 00:00:35,560
ourselves of the output, and you can see
that we've got the eight meals printed out,

7
00:00:35,560 --> 00:00:39,880
with the spam removed. Each line of
the output is enclosed in square

8
00:00:39,880 --> 00:00:44,059
brackets, and each string in the list is
enclosed in a single quote - that's how

9
00:00:44,059 --> 00:00:50,210
Python prints lists by default. Now we
can use join to tidy this up, so let's have

10
00:00:50,210 --> 00:00:56,180
a go of doing that. I'm going to change
line 17 to type, in those parentheses,

11
00:00:56,180 --> 00:01:01,309
double quotes comma and a space and
double quotes. Then I'm going to type dot

12
00:01:01,309 --> 00:01:10,280
join, and then meal in parentheses. So
let's run that and then I'll explain

13
00:01:10,280 --> 00:01:16,159
what it's doing. So you can see that, now
that we've run that, the output was what

14
00:01:16,159 --> 00:01:20,299
we wanted at the end of the previous
video. The items in each meal are printed

15
00:01:20,299 --> 00:01:25,520
on the same line, separated by a comma
and a space. Now the comma and space

16
00:01:25,520 --> 00:01:30,890
come from the string that we called join
on, on line 17. This example might be a

17
00:01:30,890 --> 00:01:35,149
bit confusing. We can see that join is
useful, but you may find it hard to

18
00:01:35,149 --> 00:01:40,039
understand what it's doing, because we're
using it inside a loop. Let's see some

19
00:01:40,039 --> 00:01:44,299
simpler examples. I'm going to create a
new Python file. I don't want to call

20
00:01:44,300 --> 00:01:47,780
the file join, because that's the name of
the method we're using.

21
00:01:47,780 --> 00:01:52,300
Avoid using Python names for your files
because it can cause problems. Right, so

22
00:01:52,310 --> 00:01:56,859
I'm gonna call this one joining_things.py.

23
00:02:04,460 --> 00:02:08,580
We're gonna start by creating a list and
then printing out its items. So I'm going

24
00:02:08,580 --> 00:02:13,820
to type flowers is equal to, left square
bracket, and on the next line I'm going

25
00:02:13,830 --> 00:02:21,120
to start typing , I'll start
with Daffodil comma, next line Evening

26
00:02:21,120 --> 00:02:32,140
Primrose comma - obviously, these are in
double quotes, next line Hydrangea,

27
00:02:32,140 --> 00:02:37,140
comma to go to the next line, Iris on the next
line comma. Next line,

28
00:02:37,140 --> 00:02:52,120
Lavender, next line, Sunflower, next line,
one last one, Tiger Lily.

29
00:02:52,120 --> 00:02:58,040
Then we'll print them out by typing for flower in flowers:

30
00:02:58,040 --> 00:03:06,740
print flower, and there's no surprises
when I run that. We get each of the

31
00:03:06,750 --> 00:03:11,430
flowers printed out. What we've done
there is, iterated over the list and

32
00:03:11,430 --> 00:03:17,160
printed each item. The join method
actually takes care of iterating for us.

33
00:03:17,160 --> 00:03:22,500
We give it an iterable and it joins all
the items in the iterable. It uses the

34
00:03:22,500 --> 00:03:28,169
string we call it on as the separator.
Now a list is an iterable, so we can pass

35
00:03:28,169 --> 00:03:32,370
our flowers list to the join method. So
what I'm going to do is comment out this

36
00:03:32,370 --> 00:03:42,510
for loop. Alright, so I'm going to type
separator is equal to double quotes - I'm

37
00:03:42,510 --> 00:03:48,720
going to use a pipe character, with a
space to the left and right of it.

38
00:03:48,720 --> 00:03:52,200
Then I'm going to type, on the next line, output is
equal to separator

39
00:03:52,200 --> 00:04:01,310
dot join, then in parentheses, flowers.
Then I'm going to print output.

40
00:04:02,140 --> 00:04:05,890
Now we could write all that on one line,
but I've split each part up to make it

41
00:04:05,890 --> 00:04:10,780
easier to understand. Join is a string
method, and we start with a string

42
00:04:10,780 --> 00:04:16,640
on line 14. I called a string separator, so
it's clear how the string is used.

43
00:04:16,640 --> 00:04:21,940
On line 15, we call the join method. That
will iterate over the flowers list and

44
00:04:21,940 --> 00:04:29,080
create a string containing each flower,
separated by space bar space. The bar is

45
00:04:29,080 --> 00:04:32,140
also known as pipe - I refer to that as
the pipe character bur bar is

46
00:04:32,140 --> 00:04:36,610
another word for it. Now I'm using dot
notation there. Join is a method of the

47
00:04:36,610 --> 00:04:44,860
str class, and separator is a string. So
if I run the program now, and that's the

48
00:04:44,860 --> 00:04:49,620
output we get. Each of the flowers has
been joined into a single string,

49
00:04:49,620 --> 00:04:55,140
with space bar space between each one. Now we
could use a comma and a space instead,

50
00:04:55,140 --> 00:05:00,940
so let's change that. Remove one of the
spaces, then just change that to a comma

51
00:05:00,940 --> 00:05:07,960
space. Run that. This time, as you can
see, there's a comma and a space between

52
00:05:07,960 --> 00:05:13,479
each flower. Now note that we haven't had
to use a for loop here. Join iterates

53
00:05:13,479 --> 00:05:18,400
over the list for us. Alright, so that's
the join method. It creates a string from

54
00:05:18,400 --> 00:05:23,320
a list, and separates each item with the
string that it's called on. So experiment

55
00:05:23,320 --> 00:05:27,720
with different lists and strings.
Remember you can also use string literals,

56
00:05:27,720 --> 00:05:35,520
so you could type, line 18,
print parentheses, comma in double

57
00:05:35,520 --> 00:05:47,300
quotes, then dot join, and in parentheses,
flowers. If you run that,

58
00:05:47,300 --> 00:05:52,160
and you can see that's separated each
flower with just a comma. Now before I

59
00:05:52,160 --> 00:05:55,820
finish the video, there is one thing to
be aware of - all the items in the

60
00:05:55,820 --> 00:06:01,010
iterable must be strings, if you want to
join the items. For example, if I go back

61
00:06:01,010 --> 00:06:09,220
up to our list, and I add a number - an int -
to our list down here,

62
00:06:09,220 --> 00:06:15,680
the code will actually crash. So if we run that, you can
see we've got an error. Join can't join

63
00:06:15,680 --> 00:06:20,270
ints to the string that it produces. Alright, so let's finish the video now, with

64
00:06:20,270 --> 00:06:24,110
a quick look at what the documentation
says about lists. I'm going to paste the

65
00:06:24,110 --> 00:06:27,340
link into a into the browser.

66
00:06:33,590 --> 00:06:38,310
Right, so the first paragraph says that
lists are typically used to store

67
00:06:38,310 --> 00:06:44,380
collections of homogeneous items.
Homogeneous, here, means items of the same type.

68
00:06:44,380 --> 00:06:48,800
Our original list was homogeneous
because all the items were strings.

69
00:06:48,800 --> 00:06:55,420
So I'll switch back to our code, so we can
see our list again.

70
00:06:55,420 --> 00:06:59,660
So lists are typically used to store homogeneous
items, but you are free to store just

71
00:06:59,670 --> 00:07:03,210
about anything you want in them. Just be
aware of the problems that can cause,

72
00:07:03,210 --> 00:07:07,530
when you come to process the list. Our
code worked fine when all the items were

73
00:07:07,530 --> 00:07:11,550
strings, but you saw it crash when we
added in integer value. Now this is

74
00:07:11,550 --> 00:07:16,280
something we often get asked about, so
I'm taking the opportunity to clarify it here.

75
00:07:16,280 --> 00:07:19,800
Python will let you store different
types in a list. What the documentation

76
00:07:19,800 --> 00:07:25,170
is saying, is that lists are intended to
be homogeneous, but that's not enforced

77
00:07:25,170 --> 00:07:27,870
by the compiler. Sometimes there's a good case for

78
00:07:27,870 --> 00:07:32,190
storing different types in a list - just
be aware that you may run into problems

79
00:07:32,190 --> 00:07:36,060
processing the items in a list, if
they're not all of a similar type.

80
00:07:36,060 --> 00:07:40,520
Alright, so let's end the video here. In the
next one, we'll look at the opposite of join

81
00:07:40,520 --> 00:07:45,080
- the split method. See you in the
next video.

