Syntax of the find() method
- How to use
find() with no start and end parameters example
- How to use
find() with start and end parameters example
- Substring not found example
- Is the
find() method case-sensitive?
find() vs in keyword
find() vs index()
The find() Method – A Syntax Overview
The find() string method is built into Python’s standard library.
It takes a substring as input and finds its index – that is, the position of the substring inside the string you call the method on.
The general syntax for the find() method looks something like this:
string_object.find("substring", start_index_number, end_index_number)
Let’s break it down:
string_object is the original string you are working with and the string you will call the find() method on. This could be any word you want to search through.
- The
find() method takes three parameters – one required and two optional.
"substring" is the first required parameter. This is the substring you are trying to find inside string_object. Make sure to include quotation marks.
start_index_number is the second parameter and it’s optional. It specifies the starting index and the position from which the search will start. The default value is 0.
end_index_number is the third parameter and it’s also optional. It specifies the end index and where the search will stop. The default is the length of the string.
- Both the
start_index_number and the end_index_number specify the range over which the search will take place and they narrow the search down to a particular section.
The return value of the find() method is an integer value.
If the substring is present in the string, find() returns the index, or the character position, of the first occurrence of the specified substring from that given string.
If the substring you are searching for is not present in the string, then find() will return -1. It will not throw an exception.
How to Use find() with No Start and End Parameters Example
The following examples illustrate how to use the find() method using the only required parameter – the substring you want to search.
You can take a single word and search to find the index number of a specific letter:
fave_phrase = "Hello world!"
# find the index of the letter 'w'
search_fave_phrase = fave_phrase.find("w")
print(search_fave_phrase)
#output
# 6
I created a variable named fave_phrase and stored the string Hello world!.
I called the find() method on the variable containing the string and searched for the letter ‘w’ inside Hello world!.
I stored the result of the operation in a variable named search_fave_phrase and then printed its contents to the console.
The return value was the index of w which in this case was the integer 6.
Keep in mind that indexing in programming and Computer Science in general always starts at 0 and not 1.
How to Use find() with Start and End Parameters Example
Using the start and end parameters with the find() method lets you limit your search.
For example, if you wanted to find the index of the letter ‘w’ and start the search from position 3 and not earlier, you would do the following:
fave_phrase = "Hello world!"
# find the index of the letter 'w' starting from position 3
search_fave_phrase = fave_phrase.find("w",3)
print(search_fave_phrase)
#output
# 6
Since the search starts at position 3, the return value will be the first instance of the string containing ‘w’ from that position and onwards.
You can also narrow down the search even more and be more specific with your search with the end parameter:
fave_phrase = "Hello world!"
# find the index of the letter 'w' between the positions 3 and 8
search_fave_phrase = fave_phrase.find("w",3,8)
print(search_fave_phrase)
#output
# 6
Подробнее ...