Concept
Finding Words (.find() vs .index())
Both tools search for text and return its starting index position:
python
phrase = "cybersecurity"
print(phrase.find("sec")) # 5
print(phrase.index("sec")) # 5# 38. Finding Words (.find() vs .index()) Both tools search for text and return its starting index position: ```python phrase = "cybersecurity" print(phrase.find("sec")) # 5 print(phrase.index("sec")) # 5 ``` The difference when the word is missing: - `.find("ninja")` returns `-1` (polite and gentle) - `.index("ninja")` throws a `ValueError` crash (dramatic and destructive)
Checkpoint
Why do developers prefer `.find()` over `.index()`?
Practice
Search for `"cat"` inside `text = "scattered"` using `.find()` and print the index.
01
Tap code blocks below to assemble
Available Blocks