Cheers and welcome back to another class of our course about the complete introduction to Python.
So in this class we are gonna talk about a very important thing which are strings and we are gonna talk
about different manipulations that you guys can do with the well with strings.
So as we talked in our last class strings are simply text that you want when if you want to add text
to your code to when you are programming.
Strings are simply a way that you can add the text on your let's say on your code when you are programming
something.
And this is why it is very important to understand how we can work with them what exactly we can do
with them and what are the different manipulations that we can do with them.
So in this course how are divided all this.
We are gonna talk about the first thing storing data in our variables.
So storing strings in our variables we are gonna talk about the string storage after that.
Well will talk about the storage and how we can manipulate it after what we are going to talk about
functions.
So what is some different functions.
There are hundreds of functions that exist for strings but we are going to talk about the most common
ones.
So in this case if I'm not mistaken we have four of them.
And finally we are gonna talk about a small little thing that you guys can do.
Once again you'll see this by the end of this class.
So my goal right now is simply to present you what you can do with strings.
Once again it's going to be some basic manipulations but you guys will see it's gonna be really cool.
So let's start.
So the first thing I'll ask you guys to do is simply open up your by charm or your.
Any text editor that you guys use to be able to run Python when it's all done.
The first thing we will write down is simply a small sentence or in this case will write down print.
Let's say I love pizza.
So we will print.
I love pizza
in our goal right now.
Simple simply just run it so you can see we have our way hour our string which is I love pizza and we
have our small app that is printed right here so our app we asked Python to print.
I love pizza but well you can also do we you can store this instead of printing.
I love pizza.
We can store this string into a variable so let's say for example we want to create a variable that
we will name sentence and we want to store it in it.
I love pizza so we store.
I love pizza all right.
And we can run it so we can see we have I love pizza first.
So just create our second function so in this case we want to run.
All right.
So as you can see in the first case scenario we print I love pizza and we run.
I love pizza so the app is simply asking Python to run this text the second so in the second thing right
here.
So if we run it in both cases it's reading.
I love pizza.
But in the second part right here what we ask Python to do is add first name sentence.
I love pizza so we going to store in the variable sentence the data.
I love pizza and then we ask Python to run this variable which refers to I love pizza.
So you get the difference.
So instead of writing down the whole thing each and every time we can simply store it into a variable
and ask Python to run it right after that.
All right.
So we'll just delete the first part of the code when it's all done.
The next thing that we will talk about so right now you understand the data storage.
So even if we change the I love pizza instead we write down.
I love pizza and let's say nachos and we run our code.
You can see that python will still print our sentence which is I love pizza and nachos.
All right.
So I know that you guys understand the first part which is storing data into a variable.
Well storing a string into a variable we're going to talk about the second part which is in this case
functions so functions are a very important part in string manipulation and we are going to talk about
some basic functions that we are going to use for discourse.
So you'll see it's not that hard to understand.
You can do some cool stuff with it.
Right so the first thing that we will talk about will be our first function which will be the upper
function.
So in this case what we want to do is simply take all the characters of this will of this sentence which
is I love the work of the string and we want to put them all into upper characters.
So how we'll do this.
It's very simple.
We'll write we have right here our Well what we ask.
Bite onto the print and we'll ask him to print sentence.
So at the end of sentence simply click on that and we'll write down our function.
As I said our function is the upper function.
So you just write down upper you open and close parentheses.
And when you run the app as you can see Python printed I love pizza and nachos in upper cases because
we asked Python to print our our app in upper cases.
All right.
So when it's done the next thing that I'll ask you guys to do in Sim is simply take some letters in
a while in complete randomness and put them in the upper characters
when it's all done right now what we'll see we'll see that python.
There is also a way for us to ask Python to put all this sentence in lower characters.
In this case how this will work.
It's simply another function that we'll use.
So how do we read this function.
We will write it down the exact same way.
So let's start from the beginning.
We have our print sentence.
So this is the code that will ask Python to execute.
We have our sentence we'll write down dot and then our second function will be lower so lower and you
open and close parentheses.
And then the next thing that you guys will do is simply run the app so you can see I love pizza and
nachos is on in Lower cheese.
All right.
Amazing so right now you know how to put all the characters in your in your python string in upper or
lower case Larry.
Now let's say that you guys want to verify if all characters are in lower character so you can ask.
There is a way that you can ask Python to verify this so you can ask python is all the characters right
here are in low are lower chapters.
So how we will do this instead of writing lower we simply will write down is lower.
So this is showing up.
So you simply write don't print the sentence.
Dot is lower.
So right now you ask Python if all those characters are in lower character.
So we'll run the up.
And as you can see Python returns us false which means that all the characters are not in lower chases.
All right.
But right now let's verify it.
If we put all the characters in lowercase is sold.
This is very simple.
We'll call it.
We'll have our right here print.
So let's do it step by step.
So we have our first part of the code which is print sentence.
We ask Python to print this part of the code.
So the first thing we'll do we'll put everything that is here in lower case is how we'll do this.
Simply write down sentence dot lower.
So the first part is putting everything in lower cases.
So we have our first part of the the app.
The second part will be asking Python if all those if all the text in this string in this variable is
in lowercase i will do this.
We'll simply ask him is lower and we open in close parenthesis.
So when it's all done we run our app.
And as you all right.
Don't forget to add the double parenthesis they both please.
When it's all done you can simply run your app and as you can see it returns the value.
True.
So very simple what you what is it what exactly happened here.
You printed sentence then you asked Python to put everything that is in this variable in lower cases
and then you ask python is everything in lower cases in this in this variable.
Right.
So this is the first the thing that we will that we saw today so right now you know how to put everything
in lower or protractor and verify it.
So as you can see it's very simple to do than say well the another function that we are going to talk
about is a function to calculate the length of a certain let's say string.
So in this case this is simply another function.
It's very simple to use.
So what we'll do we'll write down once again our first part of the code which is print sentence we ask
byte on to print the variable sentence.
So we'll just try if it works.
Yes it's the works.
Amazing.
So right now what we'll do we'll work with our second segment function.
So it's the function length.
So how do we write it down.
Very simple print.
Then you write down L E N and you open up your parentage and close it.
So let me explain.
Let me explain to you what exactly happens here.
So you ask Python to print.
So you ask Python to print you right here the length of this variable so you get it first part of the
code asking Python to return you the length of this variable in this case the length of this variable
is twenty three because we have twenty three characters and I love pizza and naturals.
Let's say we write down and I don't know.
And tackles.
So we run our code and right now we have 33 characters in our sentence.
So you get the point.
All right.
Next thing that we want to do right now you know how to calculate the length.
So very simple you print.
I'll repeat it once again.
So you print the length of this variable very simple to understand it even if you change the length.
Well python will adapt and change the answer that it will give you all right.
The next the next vote the next function that we are going to talk about sorry will be the function
of replays.
Let's say you want to replace a certain variable right here by something else.
So in this case I will do this.
It's very simple.
So we have our first part of the code which is the string.
So you simply write down print you open up your parenthesis and you write down a sentence which is your
stream.
All right.
As I said the next the next function will be the replaced functions or simply click on dot replace and
you open up your parentheses.
All right.
So the first thing we'll ask will have to do is write down the word that we want to replace.
So in this case the word we want to replace will be pizza.
Let's say it's pizza.
So you open up your will to commas and you write down pizza.
So very important you need to write down the same letters that are here.
You can't use capital letters when it's not capital letters.
When you choose choose the word simply click on comma and write down the word which by which you want
to replace me.
So it's a want to replace pizza by hamburgers
and simply click on play.
So as you can see right now our app will return this.
I love hamburgers and nachos and tackles.
Since we have our variable right here and we ask Python to print all this.
So very simple to use as well.
Let me explain to you one more time.
So you ask Python to print this variable in which you will replace the word pizza by the word hamburger.
So as you can see it's simply you are you simply talking with the computer.
So you ask him to replace things.
So it's very very simple to use another variable that we owe another viable so another function that
we will talk about.
Once again it's a very simple function to use is the index function.
In this case if let's say we are looking for a certain character it will give us the number of the character.
So let me just show you this real quick.
So how will write this down.
Very simple.
We'll write down our first part of the function which is well the function will write down a first part
of the code which is print sentence.
Then we click on index.
So the index function and let's say we are looking for the letter B.
So in this case we're looking for the letter P and we play Oracle
here we go.
So as you can see it's going to be the seventh.
It's the seventh character.
Yes it's maybe not the seventh but when you calculate with python you start to calculate with the by
year the values you saw in this case we have zero one two three four five six and seven.
And yes you have to calculate the spaces also so you don't start to calculate by would 1 you start to
calculate with 0.
So this is the main difference between python and let's say when you calculate by yourself Python Keller
starts by you so the eye right here will be zero.
The space will be one the L two three four five six and the feet will be seven.
So let's say we will.
We are looking for the A.
In this case the first a and we run our up.
It's gonna be the eleventh charity.
So if we are looking for something Python can simply well find that out.
Let's go.
Also is that we can look for complete words.
Let's say we are looking for this.
And so we just write down our weird end which is a and in the letters in the in small letters and python
will return the value thirteen which is the value of the well will return to start the beginning of
our let's say in this case of our end which starts at the value thirteen.
All right.
So after all this you can simply well you can simply delete all this.
So another cool thing that I wanted to show you guys it's going to be the last thing that I'll show
you is let's say for example you have a sentence and you want to print.
Well you want to print a part of the sentence at.
On the first line and then add the second line.
So let's write down print and let's write down something.
Well once again I love pizza and nachos and backwoods.
All right.
Perfect.
So right now if we want to drop what.
Well if we run all this we have our sentence which is I love pizza and nachos and tacos.
All right let's say we want to drop the first the first part of our sentence.
We want to drop it here.
And the second part lower even lower.
So we'll do this is very simple.
You simply have to draw this line and write it in.
So when you run your code you will have.
I love pizza and nachos.
So let me just read it once again.
As you can see and let's say we want to do the exact same thing in this part of our code.
We can do with simply like this.
So if we run the code as you can see it's right now running on three different lines.
So each time that you each time that you want to ask Python to run a part of your code on another line.
It's very simple.
You simply have to put this symbol right here and the letter.
And so it will simply run your code on a little red line.
Right.
Right now you understand how you can manipulate strings.
And while some basic string manipulations we talked about some functions right now we talked about storing
variables.
Well we've talked about storing string in variables and how we can use it.
So until now you can just practice it and play with it you'll see it's very cool to use.
There are as I explained there are hundreds of other functions that you can find on the Internet with
strings if you guys are interested.
I and I what I definitely suggest you to go on Google and look for those functions and you can play
with them.
Also they're not very hard to understand and normally they use it's easy to use.
So that's it for this class guys.
And see in our next class.