Welcome back. Now, let's spend a bit more time to talk about strings.
So far, we've worked with the hello world string.
And I did say that whenever you're wrapping your text,
you typically want to use double quotation marks. However,
that's not always true.
The default is to wrap your text with double quotation marks. However,
you can actually get away by using single quotation marks.
So let me just go over here right now and
edit that.
I'm using single quotation marks. I press run
and you can see it works perfectly fine.
So you don't always have to use double quotation marks.
The natural question you might have here is OK. So when do we use single?
When do we use double? Does it really matter?
In most cases, it really comes down to your personal preference,
whether you are a single quotation user or a double quotation user,
the default most programmers tend to go with the double
quotations
and there are instances where you could actually even use both of them
in the same string
as an example.
What if I wanted to say
something like he said
hello?
Right.
So let me just go over here right now
and say,
he
said
and
going to say hello,
how would we be able to print this out successfully?
Because if we click run right now,
it's gonna give us an error message.
Ok, we need to wrap these texts, these letters in quotation marks.
But how do we go about it?
Well, we could do this.
I could go over here right now.
Add a single
quotation mark. Oops, sorry,
it has to be inside.
So the single quotation mark first.
OK.
And now for the actual words that he said hello,
I'm going to use the double quotation marks in
here to wrap the exact word hello itself.
And then
whenever actually this is pretty common in programming languages. OK?
Whenever you have like an opening quotation mark,
you want to have a closing quotation mark as well. So I'm gonna add
the single quotation mark right there.
And now if I click run,
you can see right now it actually works.
We could do the exact opposite.
I could start off with a double quotation mark. OK?
And then I'm gonna come over here right now, wrap hello
in the single quotation marks and then
add the closing
double quotation marks run and you can see it works perfectly fine.
However,
what if I wanted very long strings, right? So I do have my Epsom
generator in here. So let me just grab
this first paragraph over here. OK.
Copy, come over here.
Now,
I could actually
wrap everything as big as it is
inside of the double quotation, mac click run
and it's gonna work perfectly fine.
But what if
I actually wanted to create some space? And so, for example,
I wanted to have some space in here.
I come over here right now and add some space in
right now. It's all kind of disorganized.
I click run.
And now we're gonna have an arrow message.
It's not working anymore because Python cannot understand
the space in between the lines of text.
So
this is where
the triple quotation marks will come in. So watch this. OK.
I'm gonna add 123.
Okay. Oops, sorry about that.
That is three. OK?
And of course, I'm gonna come over here right now
and add
the close in three quotation marks. And now if I click run
and there you go,
you can see it actually works. So
there are certain instances where you may want to use triple quotation marks if
you have very specific kind of spacing.
Uh It's also typically used for like uh documentation, lighting lines of code,
things like that.
That's where you would use the triple quotation marks.
Now, we could also combine strings,
all right.
So as an example, let me quickly show you, let me create a new variable here and say X
OK
equals and then in double quotation marks, I'm going to say hello.
OK?
And then I could create a second variable Y
equals
world.
OK?
And now
if I wanted to combine both of them,
I could say print
parenthesis very simply
X plus
Y.
And now if I click run,
there you go. Hello world.
But if we wanted to add the space between hello and world, all I can do
is I can either add the space at hello right here.
So I've added the space after the o click on run, You have hello world or
I could do the opposite
and simply add the space at the beginning of W click on run
and there you go, you have the exact same result. Now let's
make this just a little bit more sophisticated, this program,
just a little bit more sophisticated.
I'm going to introduce to you a new function
called input. So so far we've been working with print.
Now let me introduce to you input. So input is basically a function that will ask
the user to provide something. OK? So
I'm going to say input and then in parenthesis, I'm going to say say something,
please.
OK.
So if I run,
oh, sorry about that. If I run the command,
you can see right now you say say something, please. And now if I say
hi, my name is Alex and I press enter,
nothing happens.
OK? Because
our program, all it does is to ask the user to say something
and that's it. It's not going to do anything with what the user has actually said
so to make this a little bit more advanced, I'm gonna go back
in here.
Let's remove this
and let's create a new variable. I will say user
response.
OK? This is going to be the name of my variable. And by the way,
let me just say something real quick.
It's good etiquette,
good programming etiquette whenever you're creating names for your variables
or your functions and you're combining two words together.
In this case right now, I'm combining user and response.
The
second word, the first letter typically should be in capital letters.
That's why the R for response here is in capital. I'm just saying, OK, so just
bear that in mind. So
here the response equals
OK?
And I'm gonna say input
and now
in brackets, I'm gonna say,
say something,
please.
OK?
However,
I'm going to come down here,
I'm going to now
use
the print
function
in brackets,
gonna say user response.
That's it
pretty straightforward.
I'm gonna go ahead right now. Run the command.
It says say something, please. So I'm gonna say
my name is Alex Press enter and there you go.
Now, it's saying my name is Alex.
So successfully introduce to you a new function which is input,
which will ask the user to provide something.
We've talked about how
to use the single quotation marks,
the double quotation marks as well as the triple quotation marks.
And I've also shown you how we can actually combine strings
by creating multiple variables and simply using the plus operator
to combine them.
So gradually,
gradually we're getting better and better at walking with Python and string.
So
thank you for watching the video. And of course, I will see you in the next class.