[no audio]
Friends here
we are going to discuss two more very, very important string
operations in your real time.
They are like 'strip' and 'split'.
So to explain this
let me open our first of all command line, then let me enter
into my Python command line.
Let's say I have a string called
New Text
"Python", okay. Then let me simply print your 'x',
[no audio]
and by mistake instead of defining in this way
I have given some " python",
unnecessarily I provide a space before starting your string,
and that space is preserving even while printing your output.
But while printing if there is any space, maybe at starting
side of your given string, or at the ending side of your given
string, like unwantedly I provide that, or by mistake
I have given that space.
I want to remove that in case if there is a space either
side of your given string, I mean towards left side
and towards right side.
See that. First observe that. You're getting your space here
and here, right. Now
let me do 'x.strip'.
Now see the output. You don't have any space on the output side.
So 'x.strip' will remove by default if there is any space
towards left, or towards right side, or extreme sides. Only
starting or at the ending, that space we'll remove by your
'strip' operation. Actually by default 'strip' will remove the
space but I want to remove something like, let's say I have
a string now called in this way, "python". Now, I want to remove
letter "p" or letter "n"
from your given string. Because "p" is there at the starting
side I want to remove that "p", then I can do 'x.strip'.
[no audio]
See that. "p" is removing. Or I want to remove "n". Now see that
"n" is removing. But if I want to remove something called "t",
it won't do because "t" is not there either at starting side
or at ending side.
So if any character or word, if it is there either at starting
side or at ending side of your given string then that will
remove. Let's say I'm going to take "python scripting is easy".
Now what I want to do is I want to remove "easy", then
'x.strip'. If "easy" is there either at starting side or at ending
side then that will remove.
Yes, it is removing.
[no audio]
Right. Now suppose if you are given string is in this way.
Now I want to remove "python" only towards right side, not at
the starting side. This "python"
I don't want to remove. I want to remove only this "python",
this is at right side, right?
So that's why what you have to do is simply 'print(x.strip,
'rightstrip', 'rstrip', remove "python". See the output.
It is removed. It is removing only at right side. If I remove and if I play
simply 'strip' operation it is going to remove your "python"
both the sides.
So same way you can remove your particular word
even at left side also by doing 'lstrip'.
So 'strip', 'lstrip', 'rstrip'. Right. Fine.
Now, let's say I have a string called
"python", something in this way. Okay.
Now I want to remove "./i". Directly I can do it 'x.strip'.
So that result I want to store into 'x' itself, suppose
'x = x.strip'.
I want to remove './i', If anything is there either
at starting side or at ending side, and that result again
I stored into my 'x'. See that.
[no audio]
And let's say I have in this way string "python".
[no audio]
Okay.
Now something called
"yy"
[no audio]
So I want to remove letter "p" and "y" in sequence.
I mean, first I can do 'x.strip', because of this you are
going to remove "p", and again on the same operation
I can do one more '.' operation, that is "y".
See that.
So I mean multiple times you can apply '.strip'
operation or combination of
[no audio]
'strip' and 'rstrip' or even 'lstrip'
also you can apply.
Likewise you can do any number of times '.strip', '.strip',
okay, based on requirement.
You just try to remember the functionality of the '.strip'
operation on your given string.
Okay. While writing our real-time scripts there we will see
how to use effectively this 'strip' operation.
Now, let me go with the next operation,
[no audio]
that is 'split'.
So if I do simply 'x.split'. See the output.
[no audio]
Wherever space is there, right, there
it is splitting your given string.
So two places you have a space,
that's why the given string is getting into three parts.
First part, second part, third part.
[no audio]
Right. Actually, this format is called a 'list'.
We have a data structure called 'list' there we'll discuss in
depth with 'list' operations.
As of now just observe 'x.split' will give you a 'list'
output. And how it is going to split? Based on space by default.
But I want to split a given string where a word called
"is" is there. Now see the output. Wherever "is" is there, there
it is going to split. Now as of now you have only one "is" that is why
this is one part, and this is one part. You are getting two parts -
first part, second part.
Let's say your 'x' is in this way, "and it is very popular".
Now in this case if I split with "is" you have "is"
here and here, so two places you have. You will get three parts -
first part, second part, third part. You're going to get a list
which consists of three values. See the output.
first value, second value, and then third value?
Right. So guys
these are the very, very important operations in your real-time -
'strip' and 'split'. Okay.
Okay guys, thank you for watching this video.
[no audio]