{"id":8653,"date":"2021-03-01T16:50:27","date_gmt":"2021-03-01T11:20:27","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8653"},"modified":"2026-01-02T04:49:25","modified_gmt":"2026-01-02T09:49:25","slug":"using-the-find-method-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/using-the-find-method-in-python-with-examples\/","title":{"rendered":"Using the find() Method in Python with Examples"},"content":{"rendered":"\n<p>In Python,&nbsp;<code>find()<\/code>&nbsp;is a built-in string method used to locate the first occurrence of a substring within a string. It is favored for safe searching because it returns a specific index rather than raising an error if the substring is missing.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Basic Syntax<\/strong><\/h2>\n\n\n\n<p>The method is called on a string object and can take up to three parameters:&nbsp;<\/p>\n\n\n\n<p>python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string.find(substring, start, end)\n<\/code><\/pre>\n\n\n\n<p>Use code with caution.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>substring<\/code><\/strong>: The text you are looking for (required).<\/li>\n\n\n\n<li><strong><code>start<\/code><\/strong>: The index where the search begins (optional, default is\u00a0<code>0<\/code>).<\/li>\n\n\n\n<li><strong><code>end<\/code><\/strong>: The index where the search ends (optional, default is the end of the string).\u00a0<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Return Values<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Index (Integer)<\/strong>: Returns the\u00a0<strong>lowest index<\/strong>\u00a0(position) where the substring first appears.<\/li>\n\n\n\n<li><strong>-1<\/strong>: Returns\u00a0<code>-1<\/code>\u00a0if the substring is\u00a0<strong>not found<\/strong>.\u00a0<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Code Examples<\/strong><\/h2>\n\n\n\n<p>python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>text = \"Python is a powerful programming language.\"\n\n<em># Basic search<\/em>\nprint(text.find(\"powerful\"))  <em># Output: 12<\/em>\n\n<em># Search for a non-existent word<\/em>\nprint(text.find(\"Java\"))      <em># Output: -1<\/em>\n\n<em># Search within a specific range (from index 20 to the end)<\/em>\nprint(text.find(\"a\", 20))     <em># Output: 27 (the 'a' in 'language')<\/em>\n\n<em># Case-sensitivity<\/em>\nprint(text.find(\"python\"))    <em># Output: -1 (lowercase 'p' does not match)<\/em>\n<\/code><\/pre>\n\n\n\n<p>Use code with caution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Key Differences from Other Methods<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th class=\"has-text-align-left\" data-align=\"left\">Feature&nbsp;<\/th><th class=\"has-text-align-left\" data-align=\"left\"><code>find()<\/code><\/th><th class=\"has-text-align-left\" data-align=\"left\"><code>index()<\/code><\/th><th class=\"has-text-align-left\" data-align=\"left\"><code>in<\/code>&nbsp;keyword<\/th><\/tr><tr><td><strong>Return if found<\/strong><\/td><td>First index (int)<\/td><td>First index (int)<\/td><td><code>True<\/code>&nbsp;(bool)<\/td><\/tr><tr><td><strong>If not found<\/strong><\/td><td>Returns&nbsp;<code>-1<\/code><\/td><td>Raises&nbsp;<code>ValueError<\/code><\/td><td><code>False<\/code>&nbsp;(bool)<\/td><\/tr><tr><td><strong>Best use case<\/strong><\/td><td>When you need the position and want to avoid errors.<\/td><td>When the item&nbsp;<strong>must<\/strong>&nbsp;exist; otherwise, an error is expected.<\/td><td>Simple presence check (if you don&#8217;t need the index).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Related Methods<\/strong><\/h2>\n\n\n\n<p><strong><code>find_all<\/code><\/strong>: Note that Python strings do not have a built-in\u00a0<code>find_all()<\/code>\u00a0method; this is typically found in libraries like\u00a0BeautifulSoup\u00a0for web scraping.\u00a0<\/p>\n\n\n\n<p>In Python, you can find the position or index of a character or substring using the <code>find()<\/code> method. The <code>find()<\/code> method is a built-in function in Python that returns the index of the first occurrence of a specified substring. If the substring is not found in the string, the <code>find()<\/code> method returns <code>-1<\/code>. In many practical coding scenarios covered in a <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\/\">Python Training Course<\/a><\/strong>, this method is commonly used for string validation, parsing, and text processing tasks. In this tutorial, we will focus on how to use the <code>find()<\/code> method and how to work with its arguments to suit different use cases. By the end of this tutorial, you will learn:<\/p>\n\n\n\n<p><strong><code>rfind()<\/code><\/strong>: Searches from the\u00a0<strong>right<\/strong>\u00a0(end of the string) and returns the highest index.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using the find() method<\/li>\n\n\n\n<li>Using the find() method with the start Argument Defined.\u00a0<\/li>\n\n\n\n<li>Using the find() Method with the start and end Arguments defined.\u00a0<\/li>\n\n\n\n<li>Using the find() method for Substrings not Present<\/li>\n\n\n\n<li>Using the rfind() method.\u00a0<\/li>\n\n\n\n<li>Using the index() method in Python.\u00a0<\/li>\n\n\n\n<li>How to calculate the total number of times a Substring Exists<\/li>\n<\/ul>\n\n\n\n<p>We will begin with its syntax.&nbsp;<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">find(sub: Text, start: Optional[int]=..., end: Optional[int]=..., \/) -&gt; int<\/pre>\n\n\n\n<p>Parameters: There are 3 parameters to be defined. Just 1 is required, the other two are optional.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Sub (required): This is the substring you wish to find().\u00a0<\/li>\n\n\n\n<li>Start (optional): You can tweak where you want the Python interpreter to begin its search. By default, it is set to 0 which means that the search begins from the first index of the string.\u00a0<\/li>\n\n\n\n<li>end (optional): This allows you to change the index where the Python interpreter begins its search and stops. By default, it is set to the length of the string.\u00a0<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Using the find() method<\/h2>\n\n\n\n<p>We will begin by simply using the find() method by defining only the substring to be found. This implies that the default values for the start and end parameters will be taken. Let\u2019s say we wish to find the index of the first \u2018s\u2019 in H2k Infosys, we can run the code below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"751\" height=\"310\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-27.png\" alt=\"\" class=\"wp-image-33740\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-27.png 751w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-27-300x124.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-27-150x62.png 150w\" sizes=\"(max-width: 751px) 100vw, 751px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'H2k Infosys'\n&nbsp;\nprint(f\"The first 's' exists at index {string.find('s')}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 's' exists at index 8\n<\/code><\/pre>\n\n\n\n<p>In the same vein, you can find substrings as words. When substrings are in the form of words, the index of the first character of the word is returned.&nbsp; Again, if the substring occurs more than once, the Python interpreter returns the index of the first occurrence. Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Python' exists at index {string.find('Python')}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\nOutput:\nThe first 'Python' exists at index 9\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the find() method with the start Argument Defined.&nbsp;<\/h2>\n\n\n\n<p>When you specify the start argument, the interpreter begins its search from that index. This means that even if the substring to be found exists in an earlier index, it will not be chosen. One important thing to point out is that although the interpreter begins its search from the integer passed, the count begins at the first index nevertheless.&nbsp;<\/p>\n\n\n\n<p>In the last example, there exists another \u2018Python\u2019 word after the 9th index, we can search for it and begin choosing a start index greater than 9. Let&#8217;s pick 10.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Python' exists at index {string.find('Python', 10)}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 'Python' exists at index 71\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the find() Method with the start and end Arguments defined.&nbsp;<\/h2>\n\n\n\n<p>Defining the start and end argument gives you more flexibility when using the find() method. It limits not only where the interpreter begins its search but where it ends. Let\u2019s take an example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Python' exists at index {string.find('Python', 0, 15)}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 'Python' exists at index 9\n<\/code><\/pre>\n\n\n\n<p>Note that this only happens when the start argument has been defined. The find() method does not accept keywords arguments but rather positional arguments so you\u2019d need to define a start argument before the end argument can work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the find() method for Substrings not Present&nbsp;<\/h2>\n\n\n\n<p>When the substring is not found. The Python interpreter returns -1. This can be useful in codes where you want to ensure the string is not present before an action is done.\u00a0<\/p>\n\n\n\n<p>Let\u2019s take an example.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Java' exists at index {string.find('Java')}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 'Java' exists at index -1\nAs seen it returns -1. This is because Java does not appear in the string defined. \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the rfind() method.&nbsp;<\/h2>\n\n\n\n<p>You may be wondering, what about if you require the index of the last string occurrence. Well, there is a method for it and it is called the rfind() method. Ther rfind() method works like the find method but returns the highest index of the substring. This implies that the index of the last substring occurrence is returned. If, for instance, we call the rfind() function on the string, \u2018Python\u2019, the index of \u2018P\u2019 in the last occurrence is returned, rather than the first occurrence in find(). Let\u2019s see a coding example.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"803\" height=\"192\" src=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-28.png\" alt=\"\" class=\"wp-image-33742\" title=\"\" srcset=\"https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-28.png 803w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-28-300x72.png 300w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-28-768x184.png 768w, https:\/\/www.h2kinfosys.com\/blog\/wp-content\/uploads\/2021\/03\/image-28-150x36.png 150w\" sizes=\"(max-width: 803px) 100vw, 803px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Python' exists at index {string.find('Python')}\")\nprint(f\"The last index 'Python is at index {string.rfind('Python')}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 'Python' exists at index 9\nThe last index 'Python is at index 71\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using the index() method in Python.&nbsp;<\/h2>\n\n\n\n<p>The find()method works exactly like the find method in that it returns the index of the first occurrence of a substring. Let\u2019s give it a shot link in previous examples with the find() method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Python' exists at index {string.index('Python')}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 'Python' exists at index 9\n<\/code><\/pre>\n\n\n\n<p>The only difference between the index() method and the find() method is that the find() method throws an error when the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Substring\" rel=\"nofollow noopener\" target=\"_blank\">substring <\/a>is not found. Remember its find() method counterpart returns -1. Let\u2019s run the two functions on the same string.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n&nbsp;\nprint(f\"The first 'Java' exists at index {string.find('Java')}\")\nprint(f\"The first 'Java' exists at index {string.index('Java')}\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nThe first 'Java' exists at index -1\nTraceback (most recent call last):\n  File \"c:\/Users\/DELL\/Desktop\/pycodes\/__pycache__\/strings.py\", line 5, in &lt;module&gt;     \n    print(f\"The first 'Java' exists at index {string.index('Java')}\")\nValueError: substring not found\n<\/code><\/pre>\n\n\n\n<p>As seen, the find() returns -1 while the index() method throws a ValueError.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to calculate the total number of times a Substring Exists<\/h2>\n\n\n\n<p>One of the fun things to do with the find() method is to calculate the total number of times a python substring occurs in a string. In this example, we will loop over the total length of the string and call the find() method. Where the string is found, the method returns 1, which is added to a counter initialized as 0. The index of the counter is then reassigned as the index + 1, where the find() method returned a 1.\u00a0<\/p>\n\n\n\n<p>This explanation is done in Python as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n#define a string to count\nfind_string = 'Python'\n&nbsp;\nstart_index = 0\ncounter = 0\n#check if the string exist, add one to the index and reassign counter\nfor x, _ in enumerate(string):\n&nbsp;&nbsp;&nbsp;&nbsp;if string.find(find_string, start_index) != -1:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter += 1\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#reassign the index (+ 1) so that the earlier substring will not be recounted\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start_index = string.find(find_string, start_index) + 1\n&nbsp;\n#print the result\nprint(f\"{find_string} exists {counter} times in the string\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nPython exists 2 times in the string\n<\/code><\/pre>\n\n\n\n<p>As seen, Python appears twice in the string. Let\u2019s assume we want to search for H2k, we simply change the find_string variable to H2k. See the code below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#check the first existence of 's' in the string defined\nstring = 'Learning Python with H2k Infosys has been fun. I will keep on learning Python'\n#define a string to count\nfind_string = 'H2k'\n&nbsp;\nstart_index = 0\ncounter = 0\n#check if the string exist, add one to the index and reassign counter\nfor x, _ in enumerate(string):\n&nbsp;&nbsp;&nbsp;&nbsp;if string.find(find_string, start_index) != -1:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;counter += 1\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#reassign the index (+ 1) so that the earlier substring will not be recounted\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start_index = string.find(find_string, start_index) + 1\n&nbsp;\n#print the result\nprint(f\"{find_string} exists {counter} time(s) in the string\")<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\nH2k exists 1 time(s) in the string\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">To conclude,&nbsp;<\/h2>\n\n\n\n<p>You have seen how to use the <code>find()<\/code> method to check the index of a substring in a list. You also learned that ther <code>find()<\/code> method returns the index of the last occurrence of a substring. In structured learning paths such as the <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\/\">Best Online Python Course<\/a><\/strong>, these string-handling methods are introduced to help learners understand real-world text processing logic. Going forward, you learned that the <code>index()<\/code> method behaves like the <code>find()<\/code> method but throws an error if the substring is not found. Finally, you discovered how to count the number of elements in a string by using the <code>find()<\/code> method effectively.<\/p>\n\n\n\n<p>If you\u2019ve got any questions, feel free to leave them in the comment section and I\u2019d do my best to answer them.&nbsp;<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python,&nbsp;find()&nbsp;is a built-in string method used to locate the first occurrence of a substring within a string. It is favored for safe searching because it returns a specific index rather than raising an error if the substring is missing.&nbsp; 1. Basic Syntax The method is called on a string object and can take up [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8659,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8653"}],"version-history":[{"count":2,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8653\/revisions"}],"predecessor-version":[{"id":33743,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8653\/revisions\/33743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8659"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}