0:00
Hello everyone, welcome to the chapter 5 that is strings
0:03
In this chapter I will be explaining the string datotypes in detail
0:08
So what are the strings? So strings are any sequence of character within either single code or double codes are considered as strings
0:18
So in most of the languages like C C++ Java, a single character within single code is treated as character data type, right
0:25
But in Python we are not having any care as a data type
0:29
we are treating it as string only. So I'm just giving you an example
0:33
For example, S is Python in single codes or S is Python in double quotes
0:38
CH is A. For all of these, whenever I am doing type that we have already seen type CASTA type of
0:45
CH that is STR, so class is string, right? In that we can define a multi-line string intervals by using triple codes or double
0:54
code. So for example you wanted to tell your class definition details. So what you generally
1:01
can do is you can define class and its name. Then column inside that with three codes, you can write multiple
1:09
lines explaining details about that class. So similarly way, just a multi-line string literals can be
1:16
treated by the three quotes and it should end with three codes. Whenever I am printing where it is
1:21
telling that this is a three line of documentation strings that we are using. So now how to access
1:28
the character of string. You can access the character of strings by using index or by using slice
1:34
operator. So index means in Python, Python is zero index. So first will be zero index. Then slicing
1:41
operation means 10 to 20 or 1 to 5 accordingly. So we will see that one by one in example. Before
1:48
that there are different functions that are used in strings. that are used to do different operations in streams and these are really important functions for your interviews
1:58
so we'll see them one by one first one is length function alien so this is used to find the number of characters in the strings what is the length of string so alien will be helpful
2:07
for example i'm writing any of the variable with having a line of strings with n number of words right so when you are doing
2:17
early enough as it will tell that it has total 36 characters inside that particular string
2:23
So now how to check the membership inside operators and we have seen there are member type membership
2:30
operation operators which are which are defined by ring in keyword right? So for example
2:35
in this particular line if you wanted to see if learning is present, what you can do is in print
2:41
function you can just check if learning in S. So that means this particular word is available
2:46
is available in S or not. It will print true if it is available and we are also known that Python is case sensitive
2:53
so learning will be different from capital L while learning. So that's why if it is matching exactly it will print true or otherwise it will print false
3:04
Then we have a comparison of strings. We can also compare these two strings
3:09
One is S1 with whatever the input that you are asking from the user and second one will
3:14
be S2. For example if both are equal it will print one function otherwise it will do another operation So now how these comparisons are performed is it will check if the alphabetically which is at the greater place
3:29
So for example, Y will be greater than T. Right? So Z will be greater than A
3:35
So accordingly the comparison operations are performed. We will also see those examples on our Jupiter known two
3:42
So now to remove the spaces from the string. To remove the spaces you can use as strip
3:47
L strip and strip. So R strip is it will just remove the right side spaces, L will remove the left side spaces and
3:55
strip function will just remove all the spaces. See here there are no place spaces whenever you are putting or checking the outputs
4:06
Then we have finding the substrings within four methods. So for example in this particular string you want to find a sub string if P is there, if V is there, if L is there
4:17
So for that we can use find method. So now inside a forward direction, you can do find
4:24
That means s dot find of v. It will just check in s and you will tell where is V present and it will just output the index of it. Right
4:35
It is at 9 position. So that p is at 9 position according to it
4:40
But it has multiple v. First one will be printing. Right? Second one is index function
4:46
Index function will take tell at what index this particular word or character is present
4:51
For example, L is present and 0th index. So then you are doing index, it will give output as 0
4:57
Similar way you can do inside a range. For example, here you are checking E between 10 to 20
5:04
10 to 20 means it will start from 10, it will end to 19
5:08
30 to 36 minutes, it will end to 35. As in Python, we know range function, this is included, this is excluded
5:17
whenever it is r find it is reverse direction backward direction it will check from that
5:23
and if at all it is only fine it will check in forward direction so whenever you are doing
5:28
r find e in 10 to 20 it will check from reverse direction it is at first it will tell okay this is
5:34
minus one for similar way for our index river direction 36 to 30 it will check and tell it is at
5:41
35 position similar way if you wanted to count the substring you can use count method
5:47
So if you wanted to count E in all of the strings, it will tell it is repeated four times
5:54
See? And also you can do with beginning and ending of the ranges
5:58
For example, you can count E with 10 to 20. So in that case, it will just count in between those ranges, right
6:08
After that, whenever you wanted to replace string with another, you have to use replace
6:12
method. For example, in this particular line of string, you wanted to replace easy with easy you just have to write s dot replace and then output will be
6:23
this way now if you wanted to split the string and do certain operations so you can use dot
6:29
split function so dot split function will be splitting the strings into the list which will
6:35
be comma separated right so we will be seeing list in next chapters and we can discuss more on that but whenever you are doing s dot split it will split the string into the comma separate it will list now for example this particular variable is having the
6:52
tuple of these three values right see why i am seeing tuple this is for tuple and this is for list
7:00
so whenever you want to join all these elements so you can just do it by dot join method so it will
7:07
be dot join method what you are joining this particular variable or elements inside this variable
7:14
you are joining and with what you wanted to join in single code you can give with what you
7:19
wanted to join so it will be joined by dot sorry dash whatever you are giving as input to the join
7:30
method so this is your output right so similar way if you wanted to change the cases of string
7:36
you can do by using different methods like upper lower swipe title and capitalize
7:42
upper will convert all the elements or all the characters of string to uppercise
7:47
lower will do the lower case swap will just change whenever whatever is in uppercise to lower
7:51
case whatever is in lower to upper case whenever i am doing title so all of the first letter
7:57
to all the words inside a string will be capitalized so that will be titled and capitalize is
8:04
just first one of all of your statement will be capitalized. So this way you can perform different
8:12
operations on strings by using different methods. Now you wanted to check if x-y-rate things are
8:19
there inside your string. For example s is this is line one. Right so in that you wanted to
8:27
check if it has alphanumeric or if you add symbols so that you can do by using these different
8:33
methods like e's alpha new that will return all characters if at all it has having
8:38
e to z e to z zero to nine is alpha only alphabets in digit only digits
8:44
is lower is upper is title and is spaces so this will tell you i will see all these
8:51
examples in Jupiter non-go this will tell you if x y the things are there inside
8:55
your strings now you can format your string as well for example this is a line of
9:01
statement which you are printing with sprint function but every time you want this values
9:07
to be given by the user so you wanted to take input from user you can just use input
9:14
function whatever the input that are there it will be stored to this variable so i am
9:20
directly taking out input as python so whenever you are using dot format method
9:26
and whenever you are using this opening and closing curly braces instead
9:31
inside that you can tell what exact values you wanted to add dynamic
9:36
Here it is name and salary. So name will be Python, Thalerie will be 1 lakh
9:42
So its output will be Python's salary is 1 lakh and his age is 35
9:47
So it is also third braces opening and closing. So similar way you can also do it by indexing
9:55
So 0, 1 and 2. So whatever you are doing at 0, 1 and 2, it will take it and give you as output
10:01
Instead of 0 and 2 you can also give it as x y and z And then similar way you can just do the values to z is equal to something x is equal to something and y different so this way you can also format your string this is also one of the important question inside interview uh so how you will dynamically type or or dynamically
10:20
you can do outputs to your screen by using different format operations right so uh with that said
10:28
Let us quickly jump to coding part. So inside the Jupyter notebook, all those examples, whatever we have seen
10:39
I have just written and I'm just doing Shift Enter to check if whatever we are saying is true
10:45
Now this is multi-line strings. I am doing print. It will tell me, give me an output
10:51
This is a multiple lines in each separate line. All these details are printed
10:56
Then by using slicing operator operations you can check if some zero is P, s of minus 2 means and minus 1, 0 is minus 2 and S of 5 means 0 1 to 3, 4, 5
11:11
If I am printing, I am getting those output. By using slicing operations, you can do from forward to backward slices
11:18
If I am doing colon, colon and 4 means from starting to end with the steps of 4
11:23
Right? Steps of 4 means first and then 1 to 3 and then 4th. So accordingly see if I am taking the outputs, all the elements only which are outputed who are in multiples of 4
11:38
Now here from 0 to 8 means 8 is excluded, so 0 to 7. So it will start from 0 index, it will input 7th index with the steps of 2. So that's why only this is merced
11:49
Similar way when I am doing colon colon minus one, so this is the easiest way to reverse your string
11:54
So whenever in interview you might be given a position to reverse your string
12:00
So this is one of the simplest way to do colon colon minus one
12:04
So everything that is written, it will be reverse printed. After that we are just doing a length of it. See all those spaces are there
12:13
So still I am just doing this length operation if I'm doing it now
12:17
These spaces are also counted in length operation. Then I am saying this is membership operations
12:23
If this is present, then we are checking the different inputs see if at all comparing the strings
12:31
If I am giving Python first and then I am giving P-W-Y, the second should be greater than
12:39
first one. So see this way we can compare the strings. Then we are just removing the spaces at the front by the strip operation
12:47
So all those spaces are removed. Then we are just finding P and L index inside the strings
12:54
We can do that way. I am just counting the S.compte operations
12:59
Similar way I am replacing easy with easy. All these methods and all these examples we have seen
13:07
So I am just doing it one by one in Jupiter notebook. I can split it and convert it to the list
13:13
I can join it. I can just completely convert it to the list. completely converted to lower upper title and capitalizes case and also last is the end is formatting the strings
13:23
These were all the example of strings. That is it from this chapter. We'll see in the next chapter. Thank you