How to Find First Occurrence in String?
To find the first occurrence in a string, the following approaches can be used in Python:
Method 1: Find First Occurrence in String Using the “find()” Method
The “find()” method is applied to find/get the first occurrence of a specified/particular substring in an input string. It retrieves the “index” of the first appearance of the substring if it is present in the particular string, otherwise, it retrieves “-1”.
Syntax
In the above syntax:
- The “str” value specifies the string in which the substring should be found.
- The “sub” parameter is the portion of the string we want to find in the string.
- The “start” (optional) parameter is the starting index within the string where we want to start searching for the substring.
- The “end” (optional) parameter is the ending index within the string where we want to stop searching for the substring.
Example
Let’s use the below example code to find the first occurrence in the string:
print(string.find("Language"));
In the above code, the associated “find()” method is used to find the first occurrence by taking the specified substring from the string as an argument.
Output
The above output snippet shows that the specified string’s first occurrence is placed at index “7”.
Method 2: Find the First Occurrence in the String Using the “index()” Method
The “index()” method is used to find/get the index value of an item/element in a characters string or items list. This method retrieves the index value of the first occurrence of an element (in a list) from the left side of the list. If the element/item does not exist, it throws a “ValueError” exception.
Syntax
In this syntax:
- The “element” parameter specifies the element that is going to be searched in the list.
- The “start” and “end” parameters are not mandatory and indicate/specify where to start and end the search.
Example
The following example code is used to find the first occurrences in the string:
print(string.index("Language"))
In the above code block, the “string.index()” method takes the specified substring as a parameter and returns the index position of the first occurrence of that specified substring in the given string.
Output
This outcome shows the first occurrences i.e., “index” of a specified string in the input string.
Method 3: Find First Occurrence in String Using the “rfind()” and “rindex()” Functions
The “rfind()” and “rindex()” functions in Python are used to find the first occurrence of a specified string in the given string, starting from the right side of the string instead of the left. The “rfind()” function retrieves the index of the “1st” occurrence of the particular string, and the “rindex()” function raises an exception if the specified string does not exist/found.
Syntax
The syntax of “rfind()” function in Python is as follows:
The syntax of the “rindex()” function in Python is as follows:
In the above given syntax of “rfind()” and “rindex()”:
- The “value” parameter refers to the value to search for in the given string.
- The “start” is an optional parameter and it specifies the value from where to start the search. (Default value=“0”).
- The “end” is a not mandatory parameter and it indicates the position where to end/close the search. (Default= “string end”).
Example
In the below code, the “rfind()” and “rindex()” functions are used to find the first occurrence in the string:
print(string.rfind("Language"))
print(string.rindex("Language"))
In the above lines of code, the “rfind()” function returns the highest index of the substring “Language” from the main string starting from the right side of the string. Similarly, the “rindex()” function does exactly the same thing as “rfind()”, but it raises an exception if it cannot find the substring.
Output
The above output displays the first occurrences of the specified string in a string.
Conclusion
To find the first occurrence in a string using Python, the “find()” method, the “index()” method, or the “rfind()” and “rindex()” functions are used. The “find()” and “index()” methods return the index position of the first instances of the particular/specified string in a given string. The “rfind()” and “rindex()” functions find the first occurrence of a specified string in the given string, starting from the right side of the string instead of the left. This post offered multiple ways to find/get the first occurrence in a string utilizing appropriate examples.
from https://ift.tt/vBxGRqw
0 Comments