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 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 (\n).
And now if I go ahead and write "Hello world!" again afterwards, and I hit Run, 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 \n 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 HelloAngela,
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.
All right.
So there's two ways that you can do this.
Well 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 you can see that the print statement starts at the very beginning of this Line 2,
but what happens if I add a space before the print statement?
Now you can see I've got the editor screaming at me already.
I've got errors, I've got squiggly lines, and if I run this code as it is, I'll get something that's
called an IndentationError.
Now this is different to the previous error we had which was a SyntaxError.
Now what this error tells you is that there is an unexpected indent around this part of your code.
That's what the error message is telling me.
So we can then go and investigate and see our squiggly lines and we can hopefully realize that, "Oops,
we've added an extra space which we can easily get rid of," and it will calm everything down and get
rid of that error.
So remember, previously we were missing a closing quotation mark and we got a SyntaxError,
and in this case, we have an indentationError by either adding in an aberrant space or accidentally inserting
even a whole tab which is created using the tab key.
And all of these things will give you indentation errors.
So it's really important that you be careful when you're adding spaces in your Python code.
It doesn't matter so much when we have spaces in between the quotation marks, because that's a part
of the string, but anywhere that is a part of the code.
So in this case, anything that's not green and you go ahead and add some spaces, you might end up
with an IndentationError.
Now you might also, when you add spaces in your code, not have an IndentationError because the code
can function,
for example, if I add a space between print and the parentheses,
but in this case nothing really changes in terms of our output,
but we do get a warning because this is not really ideally how we should format our code.
So our warning tells us that according to PEP 8 and best practices, there shouldn't be whitespace before
an open parenthesis.
So we can also use that hint to get rid of any of these issues.
So the message here is be careful when you're adding spaces.
Make sure you know what it's doing,
and when you see the error, IndentationError,
remember what you learned in this lesson to be able to fix it. Now, as helpful as these warnings and
errors are, you'll notice that I'm putting a lot of emphasis on looking at the errors that come out
after we hit Run,
and that's because this comes from the Python compiler itself.
This will be the same whenever you write Python code.
However, these squiggly lines and these errors that you get when you hover over them are specific to
the editor that we're using here, which is PyCharm.
Now, if you happen to be using a different type of editor, they might not give you the same kind of
warnings, and they might not give you the same kind of errors.
And the messages will also be wildly different.
So it's always really important to learn the errors that you get back after you run your code,
and to remember these words SyntaxError or IndentationError,
and this is something that will help you no matter what tools you use, making you a more flexible developer.
And we're going to see this in practice right now because I've got a debugging exercise coming up for
you right after this lesson, and we're going to be doing it inside a different editor without the assistance
of all of these helpful squiggly lines.
So can you rely on your skills, your ability to debug when you run your code and see error messages,
and go through each of the lines of the debugging exercise one by one, and fix each problem from top
to bottom.
This word debugging actually comes from a story where back in the 80s, 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.