index() rindex() find() rfind() and count() methods for Python strings | Video - 26

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
friends here we are discussing about index find and count methods of python strings see we already know the index concept of python strings that means if we take any string in python by default python will give some positions for each and every character which are there in your string so suppose if this is the string then python will give index numbers in this way in terms of positive and negative index numbers are like this and here python is also providing some methods some operations to get the index of your particular character which is there in a given string so for that we have methods or operations like index and find now let me go with practice so that you can understand suppose i am taking my string as let's say python version 3.8.6 you can take any string and here suppose if you take capital p that is the first character in a given string that's why index is always zero but that zero you can get logically by using index or find method now first let me go with index print my string dot see this is the way how we can apply index method simply your variable in which your string is there dot index method then inside that parenthesis you have to give your required character in quotations so that quotations may be single or double or triple now let me save it and run it see the result yes you are getting 0 because p is first character now let me take let's say t now t is third character that's why you will get index as 2 yes you are getting and by default this index method will give index numbers for a given string for a given character in terms of positive but we know that we are also having negative index numbers for your characters so how can we get it negative index numbers so for that we don't have any default method or operation but logically you can get that with the help of index method see how i am going to get it simply find the length of your string first if you remember we have a length function using that we can find the length of a given string now while displaying the index of particular character just i am doing subtraction in this way your positive index minus the length of a string see you're going to get negative index numbers see if we take 6 6 is the last character in a given string so for last character in any string always index is -1 in terms of negative now see how i am going to get it so i am printing my string index of 6 by subtracting with length of that string now see the result yes you are getting minus 1 that's it fine and here whatever the string we are providing in index method that string is called basically a substring so that substring may be a single character or multiple characters i mean sequence of characters see suppose i am trying to print my string index of o n now see the result you are getting four so yn is your substring so for this substring what is the starting index in a given string that is nothing but four not only for o n you can take either single character or even multiple characters see suppose i am taking let's say python only see instead of o n i am taking python so this entire thing is one substring so for this substring what is the starting index in a given string so it is starting at very first that's why index is 0 that's it now see if you print your index of let's say simply vo you are getting index as sorry what happened okay index that's fine now see the result you are getting 4 but if you look into your given string you have two os here and here by default index method will look for this substring in a given string from left to right and while going from left to right the first occurring y is there here that's why it is going to give index for that that's it now in case if you want to look the index of o in a given string after particular index then you have to provide that index number here see i am giving let's say 5. now here your index method will look for the index of o from fifth index onwards fifth index is nothing but n character so from here onwards it will look towards right for this row now see the result yes 12 that's it not only this you can also provide range here see suppose i am giving let me comment this print my string dot index suppose i am looking for o only but i am giving range of characters i mean index range let's say start from 1 then i am giving let's say 5. now here 5 minus 1 4 right that's why start from index 1 from there onwards take 4 characters and in those 4 characters look for index of o that's it so here be clear this is starting index and this is not a ending index whatever the value you are providing here that minus starting index what is the range that many number of characters it will consider that's it and here by default index is looking for your given character from right left to right in a given string we are also having one more index method that is useful to look such you are given character from right to left so for that instead of index just take or index but you don't have l index i mean left index by default index itself it is a l index or index look for this character search for this character index position in a given string from right to left see the result now you are getting 12 by default that's it and one more important point is suppose whatever the character you are looking in a given string let's say x if that character is not present in a given string then index method will give some runtime error or exception called value error so whenever if you are doing index operation for a particular character or substring and if you get output as value error you have to understand that there is no such character in a given string there is no such substring in a given string that's it fine now let me go with find method see find method is also exactly same as index we are also having r find the only difference is if there is no given substring in a given string then find method will return will give minus 1 that's it see first let me go with simply find of x only see x character is not present in our given string that's why your find is going to give result as -1 so if you get output as minus 1 while doing operation with find method then you have to understand that that character is not present now instead of giving exception or run time error now find method will give minus 1 that's it see the result suppose if that string is there let's say y it will give index of that not only why even you can takes sequence of characters let's say y t h y n so this entire thing is one substring now your find will look for starting index of this substring in a given string that's it same as your index there is no change optionally you can also pass your range like this that's it that is the usage of find method only important point with the find method is if given substring is not present in the given string then it will give result as -1 in remaining cases both index and find methods are same even we have r find as well let me take instead of or index or find save it and run it it will look for your given substring in a given string from right to left that's it that's what you are getting that's fine now let me go with last method that is count see print my string dot count whoa so how many times your y is there in a given string two times yes you are getting that suppose i am taking x x is not present in a given string then your count is 0 that's it now let me take o n y n is there two times in a given string here and here now run it yes you are getting 2 fine but now i want to count this yn substring in a given string starting from let's say index 5 then yes you can provide optionally your starting index same as your index and find method only one time so from fifth index onwards when is there only one time that's it now one more important point is see till now we use all methods on our variable that is my string instead of that directly you can apply all these methods on a string itself see suppose i am taking string as hello dot you can take either find or index or count suppose i am taking count l see there is no variable directly i'm applying my method on string itself yes you can apply not only count even you can apply your index or index find and or find that's it so finally these all are the syntaxes for your index method and find method and finally count method that's it okay okay guys thank you for watching this video
Info
Channel: VRTechnologies For Automation
Views: 2,605
Rating: undefined out of 5
Keywords: index, find, count, rindex, rfind, methods, operations, string operations, python, python strings, python for automation, aws automation with python, cloud automation with python
Id: K1A74bDcaCM
Channel Id: undefined
Length: 11min 43sec (703 seconds)
Published: Tue Nov 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.