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 STR, 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 two 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.
One, two, three, four, five, six, seven, eight, nine.
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 value.
Well, I can grab the greet.
Variable, which is a string and use string slicing to grab well the first and then goal all the way
until the end.
Now the default already does this.
If I leave it like this, it's going to grab hello 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 the 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 a 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 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 going to go through everything because, well, 60% of these you're most likely never
going to use in your career.
I'm going to 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 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 ripple, if you hover over this, it'll actually tell
you what it does.
It returns a capitalized version of SE.
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 and 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 three.
So if I go to 012 and then look at that three.
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 BEE.
Now, the final thing I want to 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 overwrite them if we want, but we don't change them.
We either create them or destroy them.
In our case, when we do, quote, 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 I replace and I print quote to.
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.
Hope you're having fun.
Hang in there.
More to learn in the next video.