{"id":1053,"date":"2019-12-16T10:13:07","date_gmt":"2019-12-16T10:13:07","guid":{"rendered":"https:\/\/www.askpython.com\/?p=1053"},"modified":"2023-05-27T10:43:41","modified_gmt":"2023-05-27T10:43:41","slug":"check-string-contains-substring-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/check-string-contains-substring-python","title":{"rendered":"How to Check if a Python String Contains a Substring?"},"content":{"rendered":"\n<p>A substring is a sequence of characters within a String. When writing a Python program you may need to check whether a substring is present inside another string or not, there can be many reasons for that like searching and filtering data, input validation, etc.<\/p>\n\n\n\n<p>To check if a string contains a substring, we can utilise many built-in functions that Python provides us, each one with slightly different functionality.&nbsp;<\/p>\n\n\n\n<p>At the end of this tutorial, you will not only know how to check for substrings but also know a lot of new Python methods, so let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Methods to check if a Python string contains a substring<\/h2>\n\n\n\n<p>Python has various methods for this check, but we&#8217;ll be using those that are easy to implement and require only a few lines of code.<\/p>\n\n\n\n<p><strong>The following are the methods in Python to check if a string contains other substrings.<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using<a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-find\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/string\/python-string-find\" rel=\"noreferrer noopener\"> find() <\/a>method<\/li>\n\n\n\n<li>Using<a href=\"https:\/\/www.askpython.com\/python\/examples\/in-and-not-in-operators-in-python\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/examples\/in-and-not-in-operators-in-python\" rel=\"noreferrer noopener\"> in <\/a>operator<\/li>\n\n\n\n<li>Using <a href=\"https:\/\/www.askpython.com\/python\/string\/python-count-method\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/string\/python-count-method\" rel=\"noreferrer noopener\">count()<\/a> method<\/li>\n\n\n\n<li>Using <a href=\"https:\/\/www.askpython.com\/python\/list\/python-index-method\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/list\/python-index-method\" rel=\"noreferrer noopener\">index()<\/a> method<\/li>\n\n\n\n<li>Using operator.contains() method<\/li>\n<\/ol>\n\n\n\n<p>Now, let&#8217;s examine each of them separately, along with their respective syntax and examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the find() Method<\/h2>\n\n\n\n<p>The find() method is used to check whether a string contains a particular substring or not. It takes a substring as an argument and if the string contains that particular substring, it returns the starting index of the substring else it returns -1.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring.find(substring)\n<\/pre><\/div>\n\n\n<p>Here, <strong>substring<\/strong> is the string you want to search, and <strong>string <\/strong>is the string object in which you want to search.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr=&quot;Safa Mulani is a student of Engineering discipline.&quot; \nsub1=&quot;Safa&quot; \nsub2=&quot;Engineering&quot; \n\nprint(str.find(substring1)) \n\nprint(str.find(substring2))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n0\n28\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using the &#8216;in&#8217; operator<\/h2>\n\n\n\n<p>The &#8216;in&#8217; operator checks for the presence of a substring within a string by returning either True or False. If the substring is present it returns <strong>True<\/strong> else it returns <strong>False<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nsubstring in string\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr=&quot;Safa Mulani is a student of Engineering discipline.&quot; \nsub1=&quot;Safa&quot; \nsub2=&quot;Done&quot; \n\nprint(sub1 in str) \n\nprint(sub2 in str)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nTrue\nFalse\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using the count() Method<\/h2>\n\n\n\n<p>The count() method checks for the occurrence of a substring in a string. If the substring is not found in the string, it returns 0.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring.count(substring)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr=&quot;Safa Mulani is a student of Engineering discipline.&quot; \nsub1=&quot;Safa&quot; \nsub2=&quot;student&quot; \nsub3=&quot;Done&quot;\nprint(str.count(sub1)) \n\nprint(str.count(sub2))\n\nprint(str.count(sub3))\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n1\n1\n0\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using the index() Method<\/h2>\n\n\n\n<p>This method can also check for a substring&#8217;s presence in a\u00a0string. If the substring is not present in the string then it doesn&#8217;t return any value, rather it generates a <strong>ValueError<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring.index(substring)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr = &quot;Safa is a Student.&quot;\ntry :  \n    result = str.index(&quot;Safa&quot;) \n    print (&quot;Safa is present in the string.&quot;) \nexcept : \n    print (&quot;Safa is not present in the string.&quot;) \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSafa is present in the string.\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Using the operator.contains() Method<\/h2>\n\n\n\n<p>We can also use the operator.contains() method of the &#8220;operator&#8221; module to check for substrings. It takes a string and a substring as an argument and returns a boolean True if the substring is contained in that string, else it returns False.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\noperator.contains(string, substring)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport operator \n  \nstr = &quot;Safa is a Student.&quot;\n  \nif operator.contains(str, &quot;Student&quot;): \n    print (&quot;Student is present in the string.&quot;) \nelse : \n    print (&quot;Student is not present in the string.&quot;)  \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nStudent is present in the string. \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we explored five ways to check if a string contains a substring. Each method offers its own advantages and suitability for different scenarios. Among them, the best way to check whether a Python string contains another string is to use <code>for loop<\/code>, as it is easily understandable by beginners, more readable and does not require any additional methods. <\/p>\n\n\n\n<p>With this knowledge, you can efficiently check if string contains a substring in Python programming language. Additionally, there is another method, string.__contains__(), which provides a more efficient way of performing this check. For a detailed tutorial specifically covering this method, you can read &#8220;<a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-contains\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/python\/string\/python-string-contains\" target=\"_blank\" rel=\"noreferrer noopener\">Python String Contains: Check if a String Contains a Substring<\/a>&#8220;. Hope you have enjoyed reading the content.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/3437059\/does-python-have-a-string-contains-substring-method\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/3437059\/does-python-have-a-string-contains-substring-method<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A substring is a sequence of characters within a String. When writing a Python program you may need to check whether a substring is present inside another string or not, there can be many reasons for that like searching and filtering data, input validation, etc. To check if a string contains a substring, we can [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":51628,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=1053"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1053\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/51628"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=1053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=1053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=1053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}