{"id":3598,"date":"2022-12-01T20:25:56","date_gmt":"2022-12-01T20:25:56","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3598"},"modified":"2022-12-01T20:25:57","modified_gmt":"2022-12-01T20:25:57","slug":"python-string-find","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-find\/","title":{"rendered":"Python String find() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String find<\/p>\n<p>Now, we are going to learn about the find method. As the name of the method suggests,\u00a0 this method is going to return the first occurrence index of the string in the given string, if it is present, and if the string is not present, then we get -1 as the return value.<\/p>\n<p>Have a look at the below program now &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3599 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD.jpg\" alt=\"\" width=\"652\" height=\"72\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD.jpg 652w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-300x33.jpg 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String find() Method<\/h2>\n<p>As you can see, we have a string, and we are trying to find the first occurrence index of the string &#8216;pandit&#8217; in the string &#8216;Gyanipandit&#8217;. The output that we are going to get in this case, is the index 5 because that is where the string &#8216;pandit&#8217; starts in the string &#8216;Gyanipandit&#8217;. If we try to find some string that is not there in the given string, then we are going to get -1 as an output.<\/p>\n<p>Have a look at the below program as well, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3600 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-1.jpg\" alt=\"\" width=\"656\" height=\"81\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-1.jpg 656w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-1-300x37.jpg 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/p>\n<p>As you can see, in the above program now, we are trying to find the string &#8216;python&#8217;, in the string &#8216;Gyanipandit&#8217;, and in this case, we are going to get -1 as our output, since the string &#8216;python&#8217; is not present there in the string &#8216;Gyanipandit&#8217;.<\/p>\n<p>Also, we can provide some more additional arguments to the find method, like the start and end. By giving this, we are giving a range, in which we want to find the first occurrence of the string in the given string.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3601 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-2.jpg\" alt=\"\" width=\"650\" height=\"68\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-2.jpg 650w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-find-METHOD-2-300x31.jpg 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>As you can see in the above program, we have a simple string, and then we are trying to find the first occurrence of the string &#8216;el&#8217; in the given string. Also, this time, we are giving the start and end arguments. Within the specified start and end, we are trying to search for the first occurrence index of the specified string.<\/p>\n<p>So, as and when required, we can make use of the find method, to find the first occurrence index of some string inside the given string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String find Now, we are going to learn about the find method. As the name of the method suggests,\u00a0 this method is going to return the first occurrence index of the string in the given string, if it is present, and if the string is not present, then we get -1 as the return [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3598","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3598"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3598\/revisions"}],"predecessor-version":[{"id":4167,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3598\/revisions\/4167"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}