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 it.
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 all linked to this resource but there's not two too many.
As a matter of fact we've also seen ones like a B.S. 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 Lent
which stands for Lent.
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 1.
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 Lou.
Well I can grab the great variable which has 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 0 to length of
what does that mean.
Well if I click Run I still get Hello.
Because the length of Grete is nine
like so now.
This length function is quite useful and we're gonna 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.
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 while 60 percent 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 linked 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.
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 capitalized.
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 rebel if you hover over this they'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 he can see that you have different things that you can use.
For example you have lower instead of upper in 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 in fact click Run well
it tells me that yes code dot find has a B and it starts at index of three 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
separated 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 B.
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
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 change.
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 da replace to me or not to me it's creating a new string.
Now we're not assigning the string to anything.
So eventually after we printed 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.