Differences in python index () to find () of

Python found in the index () and find () functions implemented similar to Baidu still has not found the same.

First to a normal 

msg = "mynameishelie"
print(msg.index("m"))
print(msg.find("m"))
The output is:

0
0

Process finished with exit code 0

carry on
msg = "mynameishelie"
print(msg.index("L"))
print(msg.find("L"))

The output is: prompt substring not found

Traceback (most recent call last):
File "C:/Users/PycharmProjects/python/index_find.py", line 28, in <module>
print(msg.index("L"))
ValueError: substring not found

Process finished with exit code 1

Well, here come under the index using the syntax:
def index(self, sub, start=None, end=None):
# Like S.find () but raise ValueError when the substring is not found. 
It can be seen index () corresponds to the find (), but at the time did not find the substring error will affect program execution.

Let's look at the syntax find:
def find(self, sub, start=None, end=None):
The index in the Return Lowest # S WHERE IS found Sub the substring, 
# SUCH that Sub IS WITHIN Contained S. [Start: End] Optional The
. # Start and End are arguments in Interpreted AS Slice Notation
. # -1 the Return ON failure
and index ( ) except that the find () at the time could not find substring not throw an exception, but will return -1, and therefore will not affect the execution of the program.

Guess you like

Origin www.cnblogs.com/romeo614/p/11702752.html