Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,160 --> 00:00:00,790
All right guys.
2
00:00:00,790 --> 00:00:05,230
Let's get back to learning about a few more things that we can do with strings.
3
00:00:05,950 --> 00:00:10,480
You saw in the last lesson that if we wanted to print things on individual
4
00:00:10,480 --> 00:00:14,830
lines, then we actually had to write them a few times, right?
5
00:00:14,830 --> 00:00:17,320
We had to write print, um,
6
00:00:17,350 --> 00:00:21,970
on three lines if we wanted it to be printed one, two, three,
7
00:00:22,060 --> 00:00:27,060
like so. Now in this lesson I want to show you a method where we can do the same
8
00:00:27,700 --> 00:00:31,120
thing, but by using a single print method.
9
00:00:31,270 --> 00:00:33,580
So saving ourselves a few characters.
10
00:00:34,180 --> 00:00:39,180
So the way that we would create a new line is by writing a backslash and the N
11
00:00:40,720 --> 00:00:41,553
character.
12
00:00:42,160 --> 00:00:47,160
And now if I go ahead and write hello world again afterward and I hit run,
13
00:00:49,150 --> 00:00:53,770
or you can always use the shortcut, which is command + enter on Mac or control +
14
00:00:53,770 --> 00:00:55,540
enter on Windows.
15
00:00:56,020 --> 00:00:59,650
And you can see that once that line of code has been executed,
16
00:01:00,100 --> 00:01:03,760
I've got hello world printed on two separate lines,
17
00:01:04,209 --> 00:01:08,290
separated by this pink \n and character,
18
00:01:08,650 --> 00:01:11,770
which gets replaced by a new line.
19
00:01:13,030 --> 00:01:16,390
So go ahead and give that a go and see if you can create another one.
20
00:01:16,540 --> 00:01:17,290
Remember though,
21
00:01:17,290 --> 00:01:22,290
it's a backslash and not a forward slash but the syntax highlighting should help
22
00:01:22,300 --> 00:01:25,740
you. All right,
23
00:01:25,800 --> 00:01:30,800
we can simply just add another \n and remember that unless you want a
24
00:01:31,080 --> 00:01:35,940
space to appear right before where your hello world appears,
25
00:01:36,210 --> 00:01:40,440
you actually don't want any gaps between each of these lines.
26
00:01:43,530 --> 00:01:46,200
There we go. We've got the same result as before,
27
00:01:46,440 --> 00:01:51,440
but now using only a single print statement and creating some new lines with the
28
00:01:52,020 --> 00:01:53,130
\n.
29
00:01:54,000 --> 00:01:59,000
Now one of the other things that we can do with strings is we can concatenate
30
00:01:59,610 --> 00:02:00,443
them.
31
00:02:00,660 --> 00:02:05,660
What this means is we combine different strings so that they will be added to
32
00:02:06,840 --> 00:02:10,380
the end of another string. Here's an example.
33
00:02:10,500 --> 00:02:15,500
Let's say that we had the word hello and I wanted to add my name to the end of
34
00:02:17,010 --> 00:02:20,640
this word to make it a single string. Well,
35
00:02:20,640 --> 00:02:25,640
I can combine two strings by simply using a plus sign. So I can write "Hello"
36
00:02:27,240 --> 00:02:31,800
+ "Angela". Now here's a question. When this runs,
37
00:02:31,890 --> 00:02:36,030
what do you think it will look like? Do you think it'll write Hello
38
00:02:36,060 --> 00:02:41,040
Angela or hello Angela, all in one word? Let's see what happens.
39
00:02:42,000 --> 00:02:47,000
You can see that these two strings have now been combined into one and there's
40
00:02:47,730 --> 00:02:52,730
no space in between because we don't have a space character anywhere in here.
41
00:02:54,030 --> 00:02:59,030
Pause the video and see if you can add a space in between.
42
00:03:00,220 --> 00:03:00,610
Alright,
43
00:03:00,610 --> 00:03:05,020
so there are two ways that you can do this or maybe three actually. You could add a
44
00:03:05,020 --> 00:03:08,080
space to the end of hello, you could add a space to
45
00:03:08,140 --> 00:03:13,140
the beginning of Angela or you can actually continue using string concatenation
46
00:03:14,110 --> 00:03:18,700
by simply adding another string in between these two
47
00:03:19,060 --> 00:03:23,500
and this one is just a space. So now when I run my code,
48
00:03:23,590 --> 00:03:28,590
you can see the space gets inserted and this long thing gets combined into a
49
00:03:29,320 --> 00:03:32,890
single string that looks like this
50
00:03:33,580 --> 00:03:37,150
once this bit of the code gets executed.
51
00:03:38,140 --> 00:03:42,880
If we think of strings as a string of connected characters,
52
00:03:43,540 --> 00:03:48,540
then string concatenation is simply taking those separate strings of characters
53
00:03:49,810 --> 00:03:53,800
and merging them into one. Now,
54
00:03:53,830 --> 00:03:58,830
this is a good point to mention that in Python programming spaces are really,
55
00:03:59,590 --> 00:04:00,640
really important.
56
00:04:01,510 --> 00:04:06,510
And what I mean by this is not so much the spaces that are inside strings like
57
00:04:07,120 --> 00:04:11,470
this one, but the spaces that you add in your code.
58
00:04:12,010 --> 00:04:15,550
So here, my print statement, if I hit the backspace,
59
00:04:15,610 --> 00:04:19,930
you can see that nothing happens. I can't go any further back.
60
00:04:20,560 --> 00:04:25,560
But if I added a space or if I added a tab using the TAB key,
61
00:04:28,810 --> 00:04:33,250
then you'll see that when I try to run my code, I actually get an error.
62
00:04:33,730 --> 00:04:37,000
And the type of error this time is not a syntax error anymore.
63
00:04:37,390 --> 00:04:39,790
It's now an indentation error.
64
00:04:40,540 --> 00:04:45,540
And it says that there's an unexpected indent at this position right at the
65
00:04:45,760 --> 00:04:50,380
beginning of the print statement. So when you're coding in Python,
66
00:04:50,470 --> 00:04:55,470
it's really important that you start off all of your code at the beginning of
67
00:04:55,930 --> 00:05:00,910
the line and that you don't accidentally have any spaces or any tabs that you've
68
00:05:00,910 --> 00:05:03,550
inserted in front of the line of code.
69
00:05:04,120 --> 00:05:06,850
And the next time you get an indentation error,
70
00:05:07,210 --> 00:05:11,620
then you'll be able to look at where that error is and hopefully you'll be able to
71
00:05:11,620 --> 00:05:16,620
either use Stack Overflow or just remember what this error message means and
72
00:05:16,840 --> 00:05:18,220
you'll be able to fix your code.
73
00:05:19,300 --> 00:05:22,060
Now this is the second error that we've come across.
74
00:05:22,150 --> 00:05:25,360
The last error was a syntax error for example,
75
00:05:25,360 --> 00:05:28,420
when we forgot to add the end double quotes.
76
00:05:29,050 --> 00:05:34,050
And this one is called a syntax error and this one is called an indentation error.
77
00:05:36,010 --> 00:05:38,530
And as I mentioned before, while you're coding,
78
00:05:38,620 --> 00:05:43,210
you are going to make lots and lots of these errors and other ones.
79
00:05:43,210 --> 00:05:45,310
This is the only sure thing that we know.
80
00:05:46,300 --> 00:05:51,070
So it's great that you're already learning about them and recognizing them.
81
00:05:51,490 --> 00:05:55,270
But how can we prevent this from happening in the first place? Well,
82
00:05:55,300 --> 00:05:59,870
paying attention while you're typing helps, but we all get tired and sometimes,
83
00:05:59,870 --> 00:06:03,650
especially when I'm coding at night, sometimes even when I look at my own name,
84
00:06:03,650 --> 00:06:07,070
it looks like it's spelled wrong and I can't figure out left and right.
85
00:06:07,100 --> 00:06:11,990
So in most cases, text editors have a lot of helpful features.
86
00:06:12,470 --> 00:06:16,340
If you go into the settings bar here and you scroll all the way to the bottom,
87
00:06:16,700 --> 00:06:20,780
you can see that there's something called Code Intelligence. And code
88
00:06:20,780 --> 00:06:25,310
intelligence is something that is going to help you auto-complete some of the
89
00:06:25,310 --> 00:06:30,310
functions like our print function here and give you some helpful information or
90
00:06:30,740 --> 00:06:32,060
hints as you type.
91
00:06:32,450 --> 00:06:37,450
So let's go ahead and change it to enabled and close down our settings pane.
92
00:06:38,780 --> 00:06:43,370
And you can see now if I, for example, forget to add my double quotes,
93
00:06:43,640 --> 00:06:48,230
then I already start to get a little red squiggly line here and a little red bit
94
00:06:48,290 --> 00:06:50,780
in the right hand side of the scroll bar.
95
00:06:51,350 --> 00:06:53,810
And if I hover over the red squiggly line,
96
00:06:54,140 --> 00:06:58,910
you can see that it gives me the error that I would get if I ran my code right
97
00:06:58,910 --> 00:07:02,450
now. And so it reminds me that, Oh yeah,
98
00:07:02,480 --> 00:07:06,500
there's maybe something about my string that's not quite right.
99
00:07:06,800 --> 00:07:11,800
And you can also just Google these error messages and see it in Stack Overflow
100
00:07:11,840 --> 00:07:14,840
to remind yourself of what the error might be relating to.
101
00:07:15,530 --> 00:07:20,060
And I can fix it without having to run my code and having it break.
102
00:07:20,480 --> 00:07:23,060
Because remember that when you're writing large programs,
103
00:07:23,090 --> 00:07:25,550
it's not just going to be one line of code.
104
00:07:25,670 --> 00:07:30,200
You can have multiple lines of code each with their own errors.
105
00:07:30,200 --> 00:07:33,380
For example, this one might be missing a double quote,
106
00:07:33,620 --> 00:07:36,260
this one might be missing a plus sign,
107
00:07:36,710 --> 00:07:39,470
this one might have the print function misspelled.
108
00:07:39,980 --> 00:07:44,510
The next one might have a indentation that's not meant to be there,
109
00:07:44,930 --> 00:07:49,070
and when you run your code like this, it'll only give you the first issue.
110
00:07:49,670 --> 00:07:51,260
Once you fix that issue,
111
00:07:51,290 --> 00:07:55,820
it'll continue to the next issue until you've fixed everything,
112
00:07:56,270 --> 00:08:00,920
and this is really painful. But with code intelligence enabled,
113
00:08:01,130 --> 00:08:05,330
every time we make an error such as an indentation error like this,
114
00:08:05,570 --> 00:08:09,260
then we'll be able to hover over it and you'll see unexpected indent.
115
00:08:09,560 --> 00:08:13,970
So we'll be able to fix that. If we forget to close off our print statement,
116
00:08:14,330 --> 00:08:18,020
then we'll get a little squiggly line next to it.
117
00:08:18,380 --> 00:08:21,500
And if we say misspelled our print statement,
118
00:08:21,890 --> 00:08:26,810
we'll also get errors telling us that undefined name or it doesn't know what
119
00:08:26,870 --> 00:08:31,870
this 'prnt' is and the syntax highlighting also changes and everything should
120
00:08:34,159 --> 00:08:38,990
all help you to avoid mistakes and write good solid code.
121
00:08:40,640 --> 00:08:43,970
So now it's time for another code challenge.
122
00:08:44,480 --> 00:08:45,830
And in this code challenge,
123
00:08:46,040 --> 00:08:51,040
I've got a whole bunch of broken code for you and your job is to debug or remove
124
00:08:53,060 --> 00:08:57,840
the bugs from the code and make the code run without any errors.
125
00:08:58,740 --> 00:09:03,740
This word debugging actually comes from a story where back in the 1980s a
126
00:09:04,530 --> 00:09:09,530
moth actually flew into one of the early computers and it got electrocuted
127
00:09:11,130 --> 00:09:14,370
unfortunately for the moth and for the programmer,
128
00:09:14,400 --> 00:09:18,540
it meant that his code wasn't performing as he would expect it to.
129
00:09:18,960 --> 00:09:22,500
So he actually had to go into the computer, pick out the moth,
130
00:09:22,950 --> 00:09:25,890
and fix the wires so that it would work again.
131
00:09:26,670 --> 00:09:30,330
Now we don't have any moths flying around in our code, thankfully,
132
00:09:30,750 --> 00:09:35,520
but what we do have to do is we have to pick out the errors so that our code will
133
00:09:35,520 --> 00:09:39,630
run in the way that we expect it to without any errors.
134
00:09:40,050 --> 00:09:45,050
So head over to the next lesson and try out your first debugging exercise.
13057
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.