Find First Occurrence in a String in Python

Find First Occurrence in a String in Python

  1. Use the find() Function to Find First Occurrence in Python
  2. Use the index() Function to Find First Occurrence in Python
  3. Use the rfind() and rindex() Functions to Find Last Occurrence in Python

This tutorial will discuss the methods to find the first occurrence of a substring inside a string in Python.

Use the find() Function to Find First Occurrence in Python

We can use the find() function in Python to find the first occurrence of a substring inside a string. The find() function takes the substring as an input parameter and returns the first starting index of the substring inside the main string.

This function returns -1 if the substring isn’t present in the main string.

string = "This guy is a crazy guy."
print(string.find("guy"));

Output:

5

We found the first occurrence of the string "guy" inside the "This guy is a crazy guy" string. The find() function returns 5 as the starting index in this example.

Note that this function also counts a blank space as a character.

Use the index() Function to Find First Occurrence in Python

Using the index()function is similar to the previously discussed find() function, as it takes the substring as an input parameter and returns the first occurrence of the substring inside the main string’s starting index.

string = "This guy is a crazy guy."
print(string.index("guy"));

Output:

5

Like the find() function, the index() function also returns 5 as the starting index of the first occurrence of the string "guy" inside "This guy is a crazy guy" string.

Use the rfind() and rindex() Functions to Find Last Occurrence in Python

The two functions discussed previously locates the substring inside the main string from left to right. If we want to locate the substring from right to left, also called the last occurrence of the substring, we can use the rfind() and rindex() functions.

These functions are similar to their counterparts discussed in the previous examples, except they look from right to left. The following code snippets show the utilization of both functions in Python.

rfind():

string = "This guy is a crazy guy."
print(string.rfind("guy"));

Output:

20

rindex():

string = "This guy is a crazy guy."
print(string.rindex("guy"));

Output:

20

We found the starting index of the last occurrence of the string "guy" inside the string "This guy is a crazy guy" using the rfind() and rindex() functions in Python.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Extract Numbers From a String in Python
  • Convert String to Datetime in Python