📎 AI Summary:
The WindowsForum thread discusses the use of the Python `str.find()` method, with the original poster seeking examples of its behavior when a substring isn't found and how to specify start and end positions. Contributors clarify that `find()` returns -1 if the substring isn't found and provides examples of using the method with different starting and ending indices. Overall, the thread offers helpful explanations and examples, confirming that `find()` returns -1 when no match is found and demonstrating how to specify search ranges.

mishrajicoder

New Member
Joined
Oct 17, 2022
Messages
6
Thread Author #1
I'm looking for examples, but I'm not finding any.
Are there any examples on the internet? I'd like to know what it returns when it can't find something, as well as how to specify the starting and ending points, which I assume will be 0, -1.
Python:
>>> x = "Hello World"
>>> x.find('World')
6
>>> x.find('Aloha');
-1
Could you please help me? But I'm not convinced.
 

Solution
1. Find a substring in a string:
>>> s = "this is a string"
>>> s.find("is")
2

2. Find a substring in a string, starting at a given index:
>>> s.find("is", 3)
-1

3. Find a substring in a string, using a given start and end index:
>>> s.find("is", 3, 6)
-1

Neemobeer

Windows Forum Team
Staff member
Joined
Jul 4, 2015
Messages
8,995
If a match isn't found it will return -1. If found it returns the starting index. Indexes start at 0 which is the beginning of the string
 

AlishS

New Member
Joined
Oct 27, 2022
Messages
8
1. Find a substring in a string:
>>> s = "this is a string"
>>> s.find("is")
2

2. Find a substring in a string, starting at a given index:
>>> s.find("is", 3)
-1

3. Find a substring in a string, using a given start and end index:
>>> s.find("is", 3, 6)
-1
 

Solution