Python String find() with Examples

Spread the love

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 e.t.c. 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

Following are quick examples of 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()

  1. The first parameter is mandatory in which we need to pass a character such that its first occurring index position is returned.
  2. The second parameter is optional, which takes an index (integer) to specify where we need to start the search. By default, it is 0.
  3. 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.

python string find
Python String 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 occurrence 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

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:

  1. “sparkby” starts from the 6th index position. So starting position 6 is returned.
  2. The first occurrence of “very” starts from the 25th index position. So starting position 25 is returned.
  3. Now we are specifying the search from the 29th position. So “very” occurrence starts from the index-30

4. 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.

Leave a Reply

You are currently viewing Python String find() with Examples