WEBVTT

0
00:00.140 --> 00:05.390
All right, guys, let's get back to learning about a few more things that we can do with strings.

1
00:05.660 --> 00:11.960
You saw in the last lesson that if we wanted to print things on individual lines, then we actually

2
00:11.960 --> 00:14.780
had to write them a few times, right?

3
00:14.780 --> 00:21.950
We had to write print on three lines if we wanted it to be printed one, two, three,

4
00:21.950 --> 00:23.000
like so.

5
00:23.120 --> 00:30.320
Now in this lesson, I want to show you a method where we can do the same thing, but by using a single

6
00:30.320 --> 00:33.830
print() method, so saving ourselves a few characters.

7
00:33.860 --> 00:41.750
So the way that we would create a new line is by writing a backslash and the n character (\n).

8
00:41.840 --> 00:50.120
And now if I go ahead and write "Hello world!" again afterwards, and I hit Run, and you can see that

9
00:50.120 --> 00:58.270
once that line of code has been executed, I've got "Hello world!" printed on two separate lines, separated

10
00:58.270 --> 01:04.480
by this \n character, which gets replaced by a new line.

11
01:05.470 --> 01:09.070
So go ahead and give that a go and see if you can create another one.

12
01:09.100 --> 01:12.910
Remember though, it's a backslash (\) and not a forward slash (/).

13
01:12.910 --> 01:15.370
But the syntax highlighting should help you.

14
01:17.770 --> 01:18.400
All right.

15
01:18.400 --> 01:26.280
We can simply just add another \n and remember that unless you want a space to appear right

16
01:26.280 --> 01:33.300
before where your "Hello world!" appears, you actually don't want any gaps between each of these lines.

17
01:35.970 --> 01:36.780
There we go.

18
01:36.780 --> 01:43.740
We've got the same result as before, but now using only a single print statement and creating some

19
01:43.740 --> 01:45.920
new lines with the \n.

20
01:46.370 --> 01:52.850
Now, one of the other things that we can do with strings is we can concatenate them.

21
01:53.030 --> 02:01.490
What this means is we combine different strings so that they will be added to the end of another string.

22
02:01.730 --> 02:03.080
Here's an example,

23
02:03.080 --> 02:11.260
let's say that we had the word Hello, and I wanted to add my name to the end of this word to make it

24
02:11.260 --> 02:12.700
a single string.

25
02:12.700 --> 02:21.400
Well, I can combine two strings by simply using a plus sign so I can write "Hello" + "Angela".

26
02:21.610 --> 02:23.350
Now here's a question,

27
02:23.350 --> 02:25.810
when this runs, what do you think it will look like?

28
02:25.810 --> 02:30.580
Do you think it'll write, Hello Angela or HelloAngela,

29
02:30.580 --> 02:31.900
all in one word?

30
02:32.350 --> 02:33.820
Let's see what happens.

31
02:34.480 --> 02:41.680
You can see that these two strings have now been combined into one, and there's no space in between

32
02:41.680 --> 02:46.210
because we don't have a space character anywhere in here.

33
02:46.420 --> 02:50.050
Pause the video and see if you can add a space in between.

34
02:52.690 --> 02:53.230
All right.

35
02:53.230 --> 02:55.330
So there's two ways that you can do this.

36
02:55.330 --> 02:59.460
Well maybe three actually, you could add a space to the end of Hello,

37
02:59.460 --> 03:06.480
you could add a space to the beginning of Angela, or you can actually continue using string concatenation

38
03:06.480 --> 03:11.490
by simply adding another string in between these two,

39
03:11.490 --> 03:14.340
and this one is just a space.

40
03:14.340 --> 03:21.750
So now when I run my code you can see the space gets inserted, and this long thing gets combined into

41
03:21.750 --> 03:25.700
a single string that looks like this

42
03:26.090 --> 03:35.840
once this bit of the code gets executed. If we think of strings as a string of connected characters,

43
03:35.870 --> 03:44.240
then string concatenation is simply taking those separate strings of characters and merging them into

44
03:44.240 --> 03:44.930
one.

45
03:45.920 --> 03:53.680
Now, this is a good point to mention that in Python programming spaces are really, really important.

46
03:53.860 --> 04:02.080
And what I mean by this is not so much the spaces that are inside strings like this one, but the spaces

47
04:02.080 --> 04:04.360
that you add in your code.

48
04:04.390 --> 04:12.550
So here you can see that the print statement starts at the very beginning of this Line 2,

49
04:12.580 --> 04:18.340
but what happens if I add a space before the print statement?

50
04:18.370 --> 04:22.660
Now you can see I've got the editor screaming at me already.

51
04:22.660 --> 04:29.350
I've got errors, I've got squiggly lines, and if I run this code as it is, I'll get something that's

52
04:29.350 --> 04:31.600
called an IndentationError.

53
04:31.630 --> 04:36.160
Now this is different to the previous error we had which was a SyntaxError.

54
04:36.190 --> 04:44.070
Now what this error tells you is that there is an unexpected indent around this part of your code.

55
04:44.070 --> 04:46.380
That's what the error message is telling me.

56
04:46.410 --> 04:54.840
So we can then go and investigate and see our squiggly lines and we can hopefully realize that, "Oops,

57
04:54.840 --> 05:01.440
we've added an extra space which we can easily get rid of," and it will calm everything down and get

58
05:01.440 --> 05:02.790
rid of that error.

59
05:02.840 --> 05:10.460
So remember, previously we were missing a closing quotation mark and we got a SyntaxError,

60
05:10.460 --> 05:20.150
and in this case, we have an indentationError by either adding in an aberrant space or accidentally inserting

61
05:20.150 --> 05:24.230
even a whole tab which is created using the tab key.

62
05:24.230 --> 05:28.160
And all of these things will give you indentation errors.

63
05:28.160 --> 05:34.640
So it's really important that you be careful when you're adding spaces in your Python code.

64
05:35.000 --> 05:40.790
It doesn't matter so much when we have spaces in between the quotation marks, because that's a part

65
05:40.790 --> 05:45.200
of the string, but anywhere that is a part of the code.

66
05:45.200 --> 05:51.080
So in this case, anything that's not green and you go ahead and add some spaces, you might end up

67
05:51.080 --> 05:53.230
with an IndentationError.

68
05:53.260 --> 05:59.530
Now you might also, when you add spaces in your code, not have an IndentationError because the code

69
05:59.530 --> 06:00.640
can function,

70
06:00.640 --> 06:04.450
for example, if I add a space between print and the parentheses,

71
06:04.450 --> 06:08.710
but in this case nothing really changes in terms of our output,

72
06:08.710 --> 06:14.740
but we do get a warning because this is not really ideally how we should format our code.

73
06:14.740 --> 06:22.020
So our warning tells us that according to PEP 8 and best practices, there shouldn't be whitespace before

74
06:22.020 --> 06:24.000
an open parenthesis.

75
06:24.000 --> 06:28.800
So we can also use that hint to get rid of any of these issues.

76
06:28.800 --> 06:33.210
So the message here is be careful when you're adding spaces.

77
06:33.210 --> 06:35.490
Make sure you know what it's doing,

78
06:35.490 --> 06:38.640
and when you see the error, IndentationError,

79
06:38.640 --> 06:47.330
remember what you learned in this lesson to be able to fix it. Now, as helpful as these warnings and

80
06:47.330 --> 06:54.770
errors are, you'll notice that I'm putting a lot of emphasis on looking at the errors that come out

81
06:54.770 --> 06:56.900
after we hit Run,

82
06:57.020 --> 07:01.430
and that's because this comes from the Python compiler itself.

83
07:01.430 --> 07:06.170
This will be the same whenever you write Python code.

84
07:06.200 --> 07:13.040
However, these squiggly lines and these errors that you get when you hover over them are specific to

85
07:13.040 --> 07:16.250
the editor that we're using here, which is PyCharm.

86
07:16.280 --> 07:22.190
Now, if you happen to be using a different type of editor, they might not give you the same kind of

87
07:22.190 --> 07:26.150
warnings, and they might not give you the same kind of errors.

88
07:26.150 --> 07:29.390
And the messages will also be wildly different.

89
07:29.390 --> 07:36.460
So it's always really important to learn the errors that you get back after you run your code,

90
07:36.460 --> 07:42.400
and to remember these words SyntaxError or IndentationError,

91
07:42.400 --> 07:49.870
and this is something that will help you no matter what tools you use, making you a more flexible developer.

92
07:49.870 --> 07:56.350
And we're going to see this in practice right now because I've got a debugging exercise coming up for

93
07:56.350 --> 08:04.140
you right after this lesson, and we're going to be doing it inside a different editor without the assistance

94
08:04.140 --> 08:06.900
of all of these helpful squiggly lines.

95
08:06.900 --> 08:15.780
So can you rely on your skills, your ability to debug when you run your code and see error messages,

96
08:15.780 --> 08:23.300
and go through each of the lines of the debugging exercise one by one, and fix each problem from top

97
08:23.300 --> 08:24.080
to bottom.

98
08:24.830 --> 08:33.140
This word debugging actually comes from a story where back in the 80s, a moth actually flew into one

99
08:33.140 --> 08:37.250
of the early computers and it got electrocuted,

100
08:37.250 --> 08:43.880
unfortunately for the moth, and for the programmer, it meant that his code wasn't performing as he would

101
08:43.880 --> 08:51.150
expect it to, so he actually had to go into the computer, pick out the moth and fix the wires so that

102
08:51.150 --> 08:52.170
it would work again.

103
08:52.560 --> 08:58.500
Now, we don't have any moths flying around in our code, thankfully, but what we do have to do is

104
08:58.500 --> 09:05.400
we have to pick out the errors so that our code will run in the way that we expect it to without any

105
09:05.400 --> 09:05.970
errors.

106
09:05.970 --> 09:11.430
So head over to the next lesson and try out your first debugging exercise.