Let's talk about strings.
Strings are a thing called a sequence.
So a sequence is really just a generic term for an ordered
set. That means you have a list of things to work off.
And so when we said, let's not do 'name', let's do 'courses
= "Python
for Everybody", this is called a string.
And so if we run 'type', not string, 'type(course)',
this will just say string, 'str'.
But behind the scenes, Python is ripping this apart.
It is saying, there's a P-y-t-h-o-n, space, f-o-r, space,
E-v-e-r-y-b-o-d-y, and it is trying to map all of it.
Now, before we get into the sequence of a string, let's actually
break down what a string is.
A string really is just a sentence.
So let me actually write that out.
A "string" is just a sentence.
That's all it is.
It can have spaces,
it can have commas, underscores, special characters,
uppercase, lowercase,
it can have pretty much anything as long as it starts with
one of three things.
A string needs to start with quotation marks.
It needs to start with either quotation marks, or apostrophes,
or a set of three quotation marks, or three apostrophes, like
this. Now, there are different reasons for using these, and
let's explore some of them right now.
So let's say I have a sentence, a 'sentence', and it says,
"What's going on?"
And when I display the 'sentence', it says, "What's going on?". Not a
problem. But we can also create this sentence with apostrophes
instead of quotation marks.
Now you can actually see the syntax highlighting here is
broken, and that's because Python says, "Oh, you're starting
with an apostrophe?
This whole thing is going to be a string, and I'm going to keep
thinking that it's a string until it matches the next apostrophe.
Now, we, as humans said, well, the last one should be over here,
but because this is a, I think it's called a conjugated
word, it has an apostrophe right in here.
And instead of saying, "What is", we said, "What's",
and this is going to break. 'invalid syntax', Python is actually
telling us exactly what it is. It's a 'SyntaxError'.
So what we can do here is use quotation marks instead, and that works.
And that continues to work.
We could also use three quotation marks or three apostrophes,
either one here is going to work.
So if I keep running these cells, you're going to see that
nothing actually changes and it continues to work.
Now, the thing that I was getting at before is, if I just
copy this over and get rid of those, that's 'SyntaxError',
because we're using the same quotations.
Well, sometimes it's not super easy to just change your quotations,
or your apostrophes to quotation marks, or to a thing called
the Docstring, which is the three in a row.
But what we can do is we can say, "Hey, Python, this one in
here, right here, is not actually the end of the string".
And we tell it that it's not the end of the string,
and to render it as a regular character by adding a slash, '\'
in front of it, and this is called escaping.
So Python now sees "\'", and says, "Okay, well,
when I render this, I'm going to make sure that "\'"
shows up as just a regular apostrophe.
So let's go ahead and and run this as well,
and it works.
Now, sometimes you're going to want a string or a sentence
on multiple lines.
That's a fairly common thing that you're going to see in
Python. And there are a few different ways we could do this.
We could say 'course = "Python", and we put that in a string,
so starting and ending with quotations, then we add a slash,
backslash, '\', and then on a new line, we could say, "for Everybody",
and let's run this.
And we can actually see that it put it all in one line because
I didn't do the spacing properly in there. There we go.
I needed that extra space inside of the string for it to
basically say, "Hey, there's one here, and there's one here,
but because they're on two different lines followed by this
little slash, '\' in the middle, put them together.
So that's one method of doing it. Another method would be
to use parentheses. So we could use an opening and closing
parentheses here.
And so let's say "Hello", make sure we add spaces this time,
"My name is", "Kalob".
Let's run that, and let's not call that 'course',
let's call that 'greeting', because that's not a course,
and that's just a terrible way to improperly name variables.
And so let's run 'greeting' in here, and we see, "Hello.
My name is Kalob".
So that's the second way, is with parentheses.
And because there's an opening and a closing bracket in here,
Python is automatically going to say, "Okay. Well, I understand
what you're trying to get out here." So you don't need to
have the slash in there.
It's just going to understand that this is a string, beside
a string, beside a string, and just puts them all together.
And when Python puts things together like that, when it puts
a string together with another string, with another string
just like this, that's called concatenation. concatenation.
Now there's one more way, and you're going to see this one a
lot in the wild,
and this is more of a thing called the Docstring.
So let's actually call this a 'doctstring', 'docstring', and
it is equal to three opening quotation marks, """ or three opening
apostrophes,''', it can be either one, and it has to end the
same way, too. That's important.
Whatever it starts with, it needs to end with """Hello.
"this", "is", "a", "docstring".
And we're going to see something a little different with this one.
"docstring"
Let's go ahead and run that.
And if I do,
I didn't name that right either,
[no audio]
And I also typoed that one. Look at that.
At least I'm consistent with my typos.
And if I put 'docstring' in here and run it, you have these
things with a '\n'.
Now, in programming, you can make newlines like this,
and you can have it displayed out.
And whenever you see a newline, and actually you see it
a lot in any code, any text editor, any website, you see
newlines all over the place.
This '\n' is a newline.
That's what it means.
It's called an invisible character, and it doesn't actually show up.
But whenever a computer recognizes that there's a '\n',
it says, "Oh, okay. Well, let's put that on a new line".
And that's what this did here.
So it said, "The docstring started,
and then there was a newline.
"Hello", newline,
"this", newline, "is", newline,
"a", newline, "docstring", newline.
But because we just wanted this as a regular string, it said,
"Hey, just so you know, there are supposed to be newlines in here".
Now, what would happen if we were to print the 'docstring'?
Instead of just displaying what the value is, let's actually
print the value of 'docstring'.
Look at that, 'print', the function
'print' looks for '\n',
and says, "Oh, there's a newline".
"Hello", there's a newline.
'this', there's a newline.
And it continues to go on and actually creates newlines for us.
Now, if you're just jumping into this video, and you haven't
watched the previous videos, a fun little fact about strings
is they are immutable,
that means they cannot be changed.
All we can do is overwrite the variable name with a new value.
Now, I mentioned that this is a sequence, and a sequence
is basically a generic term for, like, an ordered set, like a grocery list.
And so what we can do here, and this is getting a little
advanced, so you don't have to know this part,
but I'm going to demonstrate this.
Let's do 'word = "Python"'.
And let's create a for loop, 'for letter in word',
and this demonstrates that this is a sequence of letters.
This isn't just one word called "Python".
It's actually six letters, individual letters, according
to Python's internal workings.
So we can do 'print(letter)'.
And what this is going to do is print 'P-y-t-h-o-n.
Let's go ahead and run that.
Sure enough, it took every single letter and printed it out
one by one.
And that is what makes a string a sequence.