1
00:00:04,840 --> 00:00:09,370
To see some more of the things we can do
with print, what I'll do is create a

2
00:00:09,370 --> 00:00:14,010
new Python file called moreprint.py.

3
00:00:22,580 --> 00:00:27,160
The first thing we're going to do, is
print several things out at once.

4
00:00:27,160 --> 00:00:36,160
We'll start by typing name equals
double quotes Tim, line two, age equals 10,

5
00:00:36,160 --> 00:00:40,020
then on line four I'm going to type
print parentheses, and then within the

6
00:00:40,020 --> 00:00:49,400
parentheses, name comma age comma Python in
double quotes and then 2020.

7
00:00:50,280 --> 00:00:54,540
So we've got two variables, and on line
four we pass both of them, with a string

8
00:00:54,550 --> 00:01:01,750
literal and an int, to print. And when I
run this program now, you can see we get

9
00:01:01,750 --> 00:01:07,090
each object printed out. The function
definition had a single parameter, called

10
00:01:07,090 --> 00:01:13,480
*objects. Now the asterisk is
often said as star, so we'd actually say

11
00:01:13,480 --> 00:01:17,220
star objects. That means that we can
provide several values, and that's what

12
00:01:17,229 --> 00:01:22,899
we've done here, on line four. Now I wouldn't
normally use this, when displaying output

13
00:01:22,899 --> 00:01:26,409
to a user of the program. That's because
you don't have all the formatting

14
00:01:26,409 --> 00:01:31,719
options that replacement fields provide.
For printing diagnostic information,

15
00:01:31,719 --> 00:01:35,770
it is a very quick way to print out the
values of several variables. But the main

16
00:01:35,770 --> 00:01:39,639
reason I've done it here, is to show you
that a function can take several values,

17
00:01:39,640 --> 00:01:45,680
when that argument is star something, and in
this case, it was star objects.

18
00:01:45,680 --> 00:01:50,799
The sep parameter is a keyword argument we
talked about in the previous video. If we

19
00:01:50,799 --> 00:01:55,389
want to provide a value for sep, we have
to specify its name. Now that's why

20
00:01:55,389 --> 00:01:58,749
they're also called named arguments. So
let's have a go at doing that.

21
00:01:58,749 --> 00:02:03,670
So I'm going to duplicate line four and
on line 5 now, I'm going to type a comma

22
00:02:03,670 --> 00:02:09,680
after the 2020, and put sep equals, and
in double quotes I'm

23
00:02:09,680 --> 00:02:17,360
going to put a comma and a space. Its usual not to
put spaces either side of the equal sign,

24
00:02:17,360 --> 00:02:19,860
when passing a keyword argument by the way.

25
00:02:19,860 --> 00:02:24,959
Okay, so we've set sep to a comma,
followed by space. Let's run the program

26
00:02:24,959 --> 00:02:30,269
to see the diffearence. Now as you can see,
in the second line of output, each value

27
00:02:30,269 --> 00:02:35,280
is separated from the next one with a
comma and a space. This can be useful when

28
00:02:35,280 --> 00:02:40,120
writing data to a text file, and we'll be
seeing that in a later section.

29
00:02:40,120 --> 00:02:44,880
The separator's only used when you print
several things at once, as we've done here.

30
00:02:44,880 --> 00:02:48,380
If you only print a single value -
and that includes an f-string or a

31
00:02:48,389 --> 00:02:54,569
string produced using .format(), then
the separator won't be printed. The other

32
00:02:54,569 --> 00:02:58,769
keyword argument that we saw in the
documentation, was end. Now end is very

33
00:02:58,769 --> 00:03:02,400
useful in loops, so to see this I'm going
to go back to our challenge code,

34
00:03:02,400 --> 00:03:10,409
in  nospam.py. So I'll run this first,
and as you can see, each of the items is

35
00:03:10,409 --> 00:03:14,340
printed on a new line. Now, it would
be tidier to have the items for each

36
00:03:14,340 --> 00:03:20,340
meal appearing on one line. Now we can do
that by changing the value of end. So to

37
00:03:20,340 --> 00:03:24,280
do that I'm going to make a change to
line 22, and before the right parenthesis

38
00:03:24,280 --> 00:03:30,360
I'm going to type comma end equals, and
in double quotes, I'm just going to put a space.

39
00:03:30,360 --> 00:03:35,060
Setting end to a space, tells print
to print a space after printing, instead

40
00:03:35,060 --> 00:03:41,000
of a newline. Let's run the program now,
to see what effect that's had to the output.

41
00:03:41,000 --> 00:03:45,419
You can see, we've now got the items
on a single line. Now the print on line

42
00:03:45,419 --> 00:03:50,370
23 is needed, otherwise all the meals
would be on the same line. So I'm going

43
00:03:50,370 --> 00:03:57,870
to comment line 23 out, and rerun it, just
to confirm what happens. You can see the

44
00:03:57,870 --> 00:04:01,160
items are now all on one line which
is, obviously, not what we want. So I'll

45
00:04:01,160 --> 00:04:07,420
undo that and run it again.
Alright. Now we can't use sep here,

46
00:04:07,420 --> 00:04:11,920
because we've only passed one object to
print - you can see that on line 22.

47
00:04:11,920 --> 00:04:16,779
Remember that the separator's only used
between the values we print, when we pass

48
00:04:16,779 --> 00:04:20,589
more than one value to the print
function. However, we can still get a

49
00:04:20,589 --> 00:04:24,400
comma between the meal items, by putting
a comma in the string that we used for

50
00:04:24,400 --> 00:04:30,060
end, so let's do that. end is now a
comma followed by a space, as you can see there.

51
00:04:30,060 --> 00:04:35,280
Let's run the program to make sure
the output is what we expect.

52
00:04:35,280 --> 00:04:39,220
That's not bad, but did you expect that extra comma
at the end of each line? It's not

53
00:04:39,220 --> 00:04:43,990
actually easy to remove that, with this
code. In fact, the most pythonic way to

54
00:04:43,990 --> 00:04:49,960
get that output without a trailing comma,
is to use a generator expression.

55
00:05:28,860 --> 00:05:30,430
So what I'm going to do is take a copy

56
00:05:30,430 --> 00:05:37,180
of this file - nospam.py. We're going to call

57
00:05:37,180 --> 00:05:44,700
this one, nospam_gen, so we
don't mess up our original code.

58
00:05:44,700 --> 00:05:49,659
So we can replace the loop on the lines 19 to
23, with this generator code that I'm

59
00:05:49,659 --> 00:05:57,360
about to paste in. Alright so there's the
code. Let's run this,

60
00:05:58,639 --> 00:06:03,229
and this time we get the items without
the trailing comma on the ends of the

61
00:06:03,229 --> 00:06:07,940
lines. So that's pretty cool, and as you
can see, that's only three lines of code.

62
00:06:07,940 --> 00:06:12,160
Alright, so I'm not going to discuss that
code on line 20, yet. What I will do in

63
00:06:12,160 --> 00:06:16,240
the next few lectures, is talk about
some more methods that strings have.

64
00:06:16,240 --> 00:06:22,860
We'll also see what join on line 20, is doing.
See you in the next video.

