-: Welcome back.
So far we've learned a few built-in functions
that Python has.
Few built-in actions, right?
Actions that we can take on data.
And we learned about SDR.
We learned about INT.
We learned about Float
and the Type conversion that we can do.
We also learned about Type.
We also learned
about Print and these built in functions in Python.
Well, there's a few of them and I'll link to this resource
but there's not too, too many.
As a matter of fact, we've also seen ones
like ABS or Round in our Numbers video.
So Numbers had some functions that we can use,
some built-in functions.
Well, Strings also has a very useful one called Len
which stands for length.
So as you guessed it, if I type
in something like this
and I print this out and I click run
I get nine.
Because it's calculating the length
of the string in our case.
1, 2, 3, 4, 5, 6, 7, 8, 9.
The length of the string is nine.
Now, you have to be careful here
because the length itself doesn't start at zero
as we've seen with indexes.
Instead, it counts like humans do from one.
So a neat thing to do here is I can do something like this.
Let's say that we have a variable Greet that equals hello.
Well, I can grab the Greet variable,
which is a string
and use string slicing to grab, well, the first
and then go all the way until the end.
Now, the default already does this.
If I leave it like this, it's going to grab hello.
Oh, and make sure I add a bracket here.
Let's click run.
I get hello.
Now, if I do zero to length of greet
What does that mean?
Well, if I click run, I still get hello, because the length
of Greet is nine.
Like so.
Now, this length function is quite useful
and we're going to use it a lot in this course.
But I want to introduce to you the concept of built-in
functions as well as what we call built-in methods.
Hmm, what does that mean?
A built-in function had this syntax
of the word that was highlighted in blue
and then we used curly brackets to perform some action
on a data type.
However, Python also has this idea of methods.
And methods are similar to functions,
but they they are owned by something.
So for example, in Python we have string methods.
So these are methods
or actions that only strings can perform.
And don't worry, this is something
that we'll talk more about
when we talk about classes and functions.
But for now, Python, for example, has string methods.
So these are methods
that we can use specifically for strings.
And methods have a special syntax
where instead of just the word with the curly brackets,
it usually has a dot in front of it.
So dot, and then some sort of word.
And we've actually seen one format, right?
Dot format, curly brackets or brackets is a method.
And if I go to Python string methods and I scroll down
or I scroll up, we see that format.
Format specific values in a string is a method.
Now, why do we care?
Well, with this Python gives us automatic tools
that we can use on strings.
So let's explore some of these string methods.
Let's create a quote here.
And the quote is going to be to be or not to be.
Now in here with this quote,
we can use some methods on this string.
Now, I'm not gonna go through everything
because, well, 60% of these you're most
likely never going to use in your career.
I'm gonna go over the important ones that you'll see
over and over again, but I'll also link to this
so that you can see this for yourself as a reference.
Remember, as a programmer your job is not to memorize this
and read a language like you read a dictionary.
Instead it is to know that this resource exists
and it's for you to explore
and learn as a specific problem occurs.
But let's get back to the task at hand.
One thing that we can do is to use the upper method.
We use a dot and then type in upper
and then the curly brackets.
And luckily for us, our editor shows us
that this actually exists
because of the dropdown menu.
So what happens now?
If I print this,
and I click run,
look at that.
Everything gets capitalized.
To be or not to be.
There is also another one we can use called Capitalize.
What's the difference between the two?
Well, if I run, you see
that it capitalizes the beginning of the sentence.
And by the way, your editor, or in our case, our repl
if you hover over this
it'll actually tell you what it does.
It returns a capitalized version of S.
So whatever is to the left
of the dot.
And a good editor will actually
show you all the things available to you.
So as soon as you press a dot, look at that.
I see all these purple boxes
which are methods that are available to me for a string.
And you see there's a lot of them.
Now, don't worry
about these double underscores, these dunder methods.
Because this is something that we'll talk
about when we talk about classes.
But you can see
that you have different things that you can use.
For example, you have lower instead of upper
it lower cases everything.
What if I want to use find?
For example, and find simply says
hey, does B exist in the code to be or not to be?
And if I click run, well, it tells me that yes.
Quote dot find has a B, and it starts at index of 3.
So if I go to 0, 1, 2, and then look at that 3.
So we have find, which finds us the first occurrence
of a piece of text.
You also have things like Replace, where I can replace
whatever I give it as the first thing, and then separate it
to the second thing.
So you can see over here it tells me old and then comma new.
And I can replace this with me.
If I click run,
I get to me or not to me.
So this replaces all the occurrences of be.
Now, the final thing I wanna point out to you is that
if I print here, quote,
what do you think is going to happen?
I've printed here and I replaced to me, to be
or not to be with to me or not to me
quite a selfish quote.
But if I print this again on the fifth line
what do you think will happen?
Let's find out.
Whoa.
Is this what you expected?
Think about why this might happen based
on what we've learned.
Here's the thing.
Remember, strings are immutable
That is, they cannot be changed.
We can override them if we want, but we don't change them.
We either create them or destroy them.
In our case, when we do quote
dot replace to me or not to me,
it's creating a new string.
Now, we're not assigning this string to anything.
So eventually after we print it, we remove it from memory.
But if I do something like this
where I have quote
two equals quote, dot replace and I print quote two.
Well in our case, we're creating a whole new string.
We're creating a string.
But we never modify the original string
because it's immutable.
So that quote always stays the way it is.
Until we destroy it.
Until we remove it, our program ends.
This is going to exist.
All right, hope you're having fun.
Hang in there more to learn in the next video.