xxxxxxxxxx
The find() method returns the index of first occurrence of the substring
(if found). If not found, it returns -1.
message = 'Python is a fun programming language'
# check the index of 'fun'
print(message.find('fun'))
# Output: 12
xxxxxxxxxx
message = 'Python is a fun language to program with'
# check the index of 'fun'
print(message.find('fun'))
# Output: 12
xxxxxxxxxx
# Python string арга .find() нь аргумент болгон дамжуулсан мөрийн эхний тохиолдлын индексийг буцаана.
# Хэрэв ямар нэгэн тохиолдол олдохгүй бол -1-ийг буцаана.
mountain_name = "Mount Kilimanjaro"
print(mountain_name.find("o")) # Prints 1 in the console.
xxxxxxxxxx
Input:
myStr = "py py py py"
print(myStr.find("js"))
print(myStr.index("js"))
Output:
-1
Traceback (most recent call last):
File "D:\Programming\python\article\find.py", line 38, in <module>
print(myStr.index("js"))
ValueError: substring not found