When using strings, it is more convenient for us to operate strings by using some methods. Let’s select some commonly used methods to explain. The basic methods include count() method, find() method and index() method. Then we introduce related functions from three perspectives: testing method, modification method and search and replacement method.
We usually use the count() method to count the number of times an element in a string appears in the string. If it does not exist, it returns 0. If it exists, it returns the number of times it exists. The syntax format is as follows:
my_str.count(x)
my_str is the string name we want to retrieve, and x is the character we want to count.
Look at the following example:
>>>my_str'www.dotcpp.com'>>>my_str.count('w')3>>>my_str.count('.')2>>>my_str.count('p')2>>> my_str.count('*')0
The number returned is the number of times the character appears in the string. Because '*' does not exist, the return value is 0.
The find method retrieves whether the specified element is contained in the string. If the element is contained, it returns the index position of the first occurrence of the element. If the character does not exist, it returns -1. Its syntax structure is:
my_str.find(x)
my_str is the string name to be retrieved, and x is the element we are looking for.
Look at the following example:
>>>my_str'www.dotcpp.com'>>>my_str.find('w')#Find the character w0>>>my_str.find('p')#Find the character p8>>>my_str.find('m ')#Find the character m13>>>my_str.find('*')#Find the character *, because * does not exist and returns -1-1
We can also use the keyword in to query whether the specified character exists in the specified string. If the specified character exists in the string, it will return True, if it does not exist, it will return False.
Look at the following example:
>>>my_str'www.dotcpp.com'>>>'p'inmy_strTrue>>>'w'inmy_strTrue>>>'.'inmy_strTrue>>>'*'inmy_strFalse
However, this method can only know whether the element we want to access exists, but cannot obtain its location. You can choose your own method to solve it during the learning process.
The index() method is similar to the find() method. When the specified character is retrieved, the index() method will also return the index position where the character first appears. However, if it cannot be retrieved, an exception will be thrown. Its syntax format for:
my_str.index(x)
my_str is the string name to be retrieved, and x is the element to be found.
Look at the following example:
>>>my_str='www.dotcpp.com'>>>my_str.index('w')0>>>my_str.index('o')5>>>my_str.index('*')Traceback(mostrecentcalllast ):File<stdin>,line1,in<module>ValueError:substringnotfound
After finding an existing element, its index position in the string will be returned. The last '*' will report an error because it is not found.
The above three methods are their omitted formats. In standard documents, their formats are:
my_str.count(x[,start[,end]])my_str.find(x[,start[,end]])my_str.index(x[,start[,end]])
When explaining above, we did not mention the content in parentheses, because in the Python documentation ''[]'' means that part can be omitted, so this part can be omitted, but when we use it, we can still use it , start is the starting position of the index, end is the ending position of the index, but does not include end.
Let's explain the standard format through an example:
>>>my_str='www.dotcpp.com'>>>my_str.index('o',6,13)#Find o12 between 6-12>>>my_str.count('w',0,5 )#Count the number of times w exists between 0-4 3>>>my_str.find('c',3,9)#Find 77 between 3-8
The use of standard format seems complicated, but it is very convenient to use.