Python String find() function is used to find the index position of the first occurrence of the character or the first occurrence of the String or the first occurrence from the specified position etc. Note that a Python String is a set of characters and its index starts from zero. Let’s go into our article.
1. Quick Examples of string.find() Method
If you are in a hurry, below are some quick examples of the string find() method.
# Quick examples of string find()
# Consider the string
command = "visit sparkby tutorials. very very nice!"
# Find 'i' from the start
print("i occurrence - ",command.find("i"))
# Find 'i' from the 6th position
print("i occurrence - ",command.find("i",6))
# Find 'i' from the 6th position to 40th position
print("i occurrence - ",command.find("i",6,40))
# Find 'sparkby'
print("sparkby - ",command.find("sparkby"))
# Find 'very' from 29th position
print("very - ",command.find("very",29))
2. Python String find()
find()
method is used to return the index of the first occurrence of the character specified or a String specified. If the character or substring is found, it will return the position. Indexing in a String starts from 0. Returns -1 if a character or String not found. It takes three parameters sub_str
, start
, and stop
. Let’s see the syntax and parameters.
2.1 Syntax of String find()
The following is the syntax of the String find() function.
#Syntax of find()
string.find(sub_str,start,stop)
2.2 Parameters of find()
Following are the parameters of the string find() function.
string
– The string on which you are performing the search.sub_str
– The first parameter is mandatory in which we need to pass a character such that its first occurring index position is returned.start
– The second parameter is optional, which takes an index (integer) to specify where we need to start the search. By default, it is 0.stop
– The third parameter is optional, which takes an index (integer) to specify where we need to end the search. By default, it is an index of the last character of the String. In other words String length – 1.
2.3 Return of find()
If the specified character or substring exists in the string, it will return the index position of the first occurrence.
If the specified character doesn’t exist in the string, it will return -1.
3. Python String find() Example
Let’s run the examples of the Python String find()
function with a combination of parameters. For the examples below I am using the String “visit sparkby tutorials. very very nice!”. Check how this String is represented in Python with index position.
3.1 Find Character from Starting Position of String
To find the index position of the first occurrence of the character or a substring from the start, use the first parameter of find()
. This by default starts finding specified occurrences
from the starting position of the String.
# Consider the string
command = "visit sparkby tutorials. very very nice!"
print(command)
# Find 'i' from the start
print("i occurrence - ",command.find("i"))
# Find 's' from the start
print("s occurrence - ",command.find("s"))
# Find 'I' from the start
print("I occurrence - ",command.find("I"))
# Output:
# visit sparkby tutorials. very very nice!
# i occurrence - 1
# s occurrence - 2
# I occurrence - -1
Yields below output.
Explanation:
We can see that the first occurrence of the character ‘i’ is at index 1, the first occurrence of the character ‘s’ is at index 2, and the character ‘I’ doesn’t exist in the String, so -1 is returned.
3.2 Find Character from Specified Position
To find the index position of the first occurrence of the character or a substring from the start of the specified position of Python String, use the first two parameters of find()
. This by default starts finding specified occurrences from the specified starting position of the String to the end.
# Consider the string
command = "visit sparkby tutorials. very very nice!"
print(command)
# Find 'i' from the 6th position
print("i occurrence - ",command.find("i",6))
# Find 's' from the 10th position
print("s occurrence - ",command.find("s",10))
# Find 't' from the 23rd position
print("t occurrence - ",command.find("t",23))
# Output:
# visit sparkby tutorials. very very nice!
# i occurrence - 19
# s occurrence - 22
# t occurrence - -1
Explanation:
We can see that the first occurrence of the character – ‘i’ is at index-19 from the 6th position, the first occurrence of the character – ‘s’ is at index-22 from the 10th position and the character – ‘t’ doesn’t exist in the String from the 23rd index position. so -1 is returned for it.
3.3 Find Character Between Specified Positions
To find the index position of the first occurrence of the character or a substring from the specified start position and continue until the specified end position, use all three parameters of find()
.
# Consider the string
command = "visit sparkby tutorials. very very nice!"
print(command)
# Find 'i' from the 6th position to 40th position
print("i occurrence - ",command.find("i",6,40))
# Find 's' from the 7th position to 25th position
print("s occurrence - ",command.find("s",7,25))
# Find 't' from the 12th position to 23rd position
print("t occurrence - ",command.find("t",12,23))
# Output:
# visit sparkby tutorials. very very nice!
# i occurrence - 19
# s occurrence - 22
# t occurrence - 14
Explanation:
We can see that the first occurrence of the character – ‘i’ is at index-19 in between 6 and 40 positions, the first occurrence of the character – ‘s’ is at index-22 in between 7 and 25 positions, and the first occurrence of the character – ‘t’ is at index-14 in between 12 and 23 positions.
4. Python Find Substring in a String
All the above examples we have used to find the character occurrence, let’s see how to find the substring in a String.
# Consider the string
command = "visit sparkby tutorials. very very nice!"
print(command)
# Find 'sparkby'
print("sparkby - ",command.find("sparkby"))
# Find 'very'
print("very - ",command.find("very"))
# Find 'very' from 29th position
print("very - ",command.find("very",29))
# Output:
# visit sparkby tutorials. very very nice!
# sparkby - 6
# very - 25
# very - 30
Explanation:
- “sparkby” starts from the 6th index position. So starting position 6 is returned.
- The first occurrence of “very” starts from the 25th index position. So starting position 25 is returned.
- Now we are specifying the search from the 29th position. So “very” occurrence starts from the index-30
Frequently Asked Questions On Python String find() Function
The find()
method is a built-in string method in Python that is used to find the index of the first occurrence of a substring within a string. If the substring is not found, it returns -1.
The main difference is in how they handle substring not found. find()
returns -1 if the substring is not present, while index()
raises a ValueError
. So, if you’re unsure whether the substring exists, find()
might be a safer choice.
The find()
method is specific to string objects in Python. It cannot be used with non-string arguments. If you want to find the position of an element in a list or another iterable, you should use the index()
method instead. The index()
method is similar to find()
, but it works with lists and other iterable types, returning the index of the first occurrence of the specified value.
The find()
method in Python does not support regular expressions. It performs a simple substring match and does not provide the advanced pattern-matching capabilities that regular expressions offer.
By default, find()
is case-sensitive. If you want a case-insensitive search, you can convert both the string and the substring to lowercase (or uppercase) using lower()
or upper()
before calling find()
.
find()
works with Unicode strings just like it does with regular ASCII strings. It doesn’t distinguish between different character encodings.
Conclusion
In this article, you have learned how to use the Python String find()
function to get the first index of the character or substring from a String. Returns -1 if it doesn’t find the character or a substring. Indexing in a String starts from 0 and the find()
method takes three parameters sub_str
, start
, and stop
.
Related Articles
- Python String Contains
- Python Remove Empty Strings from List
- Python String Sort Alphabetically
- Python String reverse with Examples
- How to Remove Commas from a String Python
- Remove Punctuation From String Python
- Python Remove Spaces From String
- Remove Substring From Python String
- Get substring of a string in Python
- Python Remove Multiple Characters From String
- Python String isnumeric
- Python Convert List of Strings to Ints
- Convert String to Decimal in Python