I. The official definition of the find function
in the first placePython's find function is mostly used in thestring (computer science)on the handling of theAlso PythonComputer Level 2 (CB2)quiz。
define: The Python find() method detects whether a string contains the substring str, and if the beg and end ranges are specified, it checks to see if it is contained within the specified ranges, and if it contains the substring it returns the index value of beg, otherwise it returns -1.
grammatical
(str, beg=0, end=len(string))
parameters
- str--a specify the string to retrieve
- beg --- start searching, default is 0
- end --- end search, default is the length of the string
return value
Returns the beginning index value if it contains a substring, otherwise -1.
Second, find function of the detailed function to use the explanation
The first example (TIPS: you can copy the code into the idle, and then run it to see for yourself, if you can't understand this one, you can take a look at the second example again, and combine it to understand)
str = "abcdefa" print(("a")) # Finds the first occurrence of a substring in a string, starting at subscript 0, and returns the result print(("a",1)) # Finds the first occurrence of a substring in a string, starting at subscript 1: return result 6 print(("7"))# Returns not found-1
Second example (TIPS: you can dip the code into idle, run it and then look at it against my answer below)
str = "hello Best wishes to you"; str1 = "to"; print((str1)) print((str1,10)) print((str1,18)) print((str1,19)) print((str1,17,19)) print((str1,17,20)) print((str1,18,20))
The first one starts counting from the character h (subscript 0) and continues until the search stops at t. All the way through the search, it is 18 strings
So first, the first print results in 18
The second one counts up from the 10th string, and as long as it is followed by the to character, then it returns the index value of the beginning, which is 18
The third from the 18th string to start counting, just falling on the t string, from t to start retrieving, then start retrieving back to the beginning of certainly no problem, continue to return 18
The fourth one starts counting from the 19th string, that is, the space after the o of to, and searches backward from the space, then there is no desired content, so it returns -1
In the fifth print, I added the end parameter, because it is directly positioned at 19, so it is equivalent to the o retrieval is not available, so the output -1
For the sixth print I changed the end to 20, and this time the output works fine.
In the seventh print I changed 17 to 18,19 to 20 and it still outputs for the same reason as above, 6 against 1.
summarize
to this detailed use of the Python find function is introduced to this article, more related Python find function to use the content please search my previous posts or continue to browse the following related articles I hope that you will support me more in the future!