All right guys.
Let's get back to learning about a few more things that we can do with strings.
You saw in the last lesson that if we wanted to print things on individual
lines, then we actually had to write them a few times, right?
We had to write print, um,
on three lines if we wanted it to be printed one, two, three,
like so. Now in this lesson I want to show you a method where we can do the same
thing, but by using a single print method.
So saving ourselves a few characters.
So the way that we would create a new line is by writing a backslash and the N
character.
And now if I go ahead and write hello world again afterward and I hit run,
or you can always use the shortcut, which is command + enter on Mac or control +
enter on Windows.
And you can see that once that line of code has been executed,
I've got hello world printed on two separate lines,
separated by this pink \n and character,
which gets replaced by a new line.
So go ahead and give that a go and see if you can create another one.
Remember though,
it's a backslash and not a forward slash but the syntax highlighting should help
you. All right,
we can simply just add another \n and remember that unless you want a
space to appear right before where your hello world appears,
you actually don't want any gaps between each of these lines.
There we go. We've got the same result as before,
but now using only a single print statement and creating some new lines with the
\n.
Now one of the other things that we can do with strings is we can concatenate
them.
What this means is we combine different strings so that they will be added to
the end of another string. Here's an example.
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 a single string. Well,
I can combine two strings by simply using a plus sign. So I can write "Hello"
+ "Angela". Now here's a question. When this runs,
what do you think it will look like? Do you think it'll write Hello
Angela or hello Angela, all in one word? Let's see what happens.
You can see that these two strings have now been combined into one and there's
no space in between because we don't have a space character anywhere in here.
Pause the video and see if you can add a space in between.
Alright,
so there are two ways that you can do this or maybe three actually. You could add a
space to the end of hello, you could add a space to
the beginning of Angela or you can actually continue using string concatenation
by simply adding another string in between these two
and this one is just a space. So now when I run my code,
you can see the space gets inserted and this long thing gets combined into a
single string that looks like this
once this bit of the code gets executed.
If we think of strings as a string of connected characters,
then string concatenation is simply taking those separate strings of characters
and merging them into one. Now,
this is a good point to mention that in Python programming spaces are really,
really important.
And what I mean by this is not so much the spaces that are inside strings like
this one, but the spaces that you add in your code.
So here, my print statement, if I hit the backspace,
you can see that nothing happens. I can't go any further back.
But if I added a space or if I added a tab using the TAB key,
then you'll see that when I try to run my code, I actually get an error.
And the type of error this time is not a syntax error anymore.
It's now an indentation error.
And it says that there's an unexpected indent at this position right at the
beginning of the print statement. So when you're coding in Python,
it's really important that you start off all of your code at the beginning of
the line and that you don't accidentally have any spaces or any tabs that you've
inserted in front of the line of code.
And the next time you get an indentation error,
then you'll be able to look at where that error is and hopefully you'll be able to
either use Stack Overflow or just remember what this error message means and
you'll be able to fix your code.
Now this is the second error that we've come across.
The last error was a syntax error for example,
when we forgot to add the end double quotes.
And this one is called a syntax error and this one is called an indentation error.
And as I mentioned before, while you're coding,
you are going to make lots and lots of these errors and other ones.
This is the only sure thing that we know.
So it's great that you're already learning about them and recognizing them.
But how can we prevent this from happening in the first place? Well,
paying attention while you're typing helps, but we all get tired and sometimes,
especially when I'm coding at night, sometimes even when I look at my own name,
it looks like it's spelled wrong and I can't figure out left and right.
So in most cases, text editors have a lot of helpful features.
If you go into the settings bar here and you scroll all the way to the bottom,
you can see that there's something called Code Intelligence. And code
intelligence is something that is going to help you auto-complete some of the
functions like our print function here and give you some helpful information or
hints as you type.
So let's go ahead and change it to enabled and close down our settings pane.
And you can see now if I, for example, forget to add my double quotes,
then I already start to get a little red squiggly line here and a little red bit
in the right hand side of the scroll bar.
And if I hover over the red squiggly line,
you can see that it gives me the error that I would get if I ran my code right
now. And so it reminds me that, Oh yeah,
there's maybe something about my string that's not quite right.
And you can also just Google these error messages and see it in Stack Overflow
to remind yourself of what the error might be relating to.
And I can fix it without having to run my code and having it break.
Because remember that when you're writing large programs,
it's not just going to be one line of code.
You can have multiple lines of code each with their own errors.
For example, this one might be missing a double quote,
this one might be missing a plus sign,
this one might have the print function misspelled.
The next one might have a indentation that's not meant to be there,
and when you run your code like this, it'll only give you the first issue.
Once you fix that issue,
it'll continue to the next issue until you've fixed everything,
and this is really painful. But with code intelligence enabled,
every time we make an error such as an indentation error like this,
then we'll be able to hover over it and you'll see unexpected indent.
So we'll be able to fix that. If we forget to close off our print statement,
then we'll get a little squiggly line next to it.
And if we say misspelled our print statement,
we'll also get errors telling us that undefined name or it doesn't know what
this 'prnt' is and the syntax highlighting also changes and everything should
all help you to avoid mistakes and write good solid code.
So now it's time for another code challenge.
And in this code challenge,
I've got a whole bunch of broken code for you and your job is to debug or remove
the bugs from the code and make the code run without any errors.
This word debugging actually comes from a story where back in the 1980s a
moth actually flew into one of the early computers and it got electrocuted
unfortunately for the moth and for the programmer,
it meant that his code wasn't performing as he would expect it to.
So he actually had to go into the computer, pick out the moth,
and fix the wires so that it would work again.
Now we don't have any moths flying around in our code, thankfully,
but what we do have to do is we have to pick out the errors so that our code will
run in the way that we expect it to without any errors.
So head over to the next lesson and try out your first debugging exercise.