{"id":8262,"date":"2024-09-10T12:37:25","date_gmt":"2024-09-10T12:37:25","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=8262"},"modified":"2025-02-07T11:36:53","modified_gmt":"2025-02-07T11:36:53","slug":"python-string-searches","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/python-string-searches\/","title":{"rendered":"Master String Searches in Python 2025"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#What_is_a_substring\" title=\"What is a substring?\">What is a substring?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Does_Python_have_a_string_contains_substring_method\" title=\"Does Python have a string contains() substring method?\">Does Python have a string contains() substring method?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#How_to_Check_for_Substrings_in_Python_Using_%E2%80%9Cin%E2%80%9C\" title=\"How to Check for Substrings in Python Using &#8220;in&#8220;\">How to Check for Substrings in Python Using &#8220;in&#8220;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Additional_Methods_for_String_Containment\" title=\"Additional Methods for String Containment\">Additional Methods for String Containment<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#1_strfindsubstring\" title=\"1. str.find(substring)\">1. str.find(substring)<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Explanation\" title=\"Explanation\">Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#2_strindexsubstring\" title=\"2. str.index(substring)\">2. str.index(substring)<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Explanation-2\" title=\"Explanation\">Explanation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#3_strstartswithprefix_and_strendswithsuffix\" title=\"3. str.startswith(prefix) and str.endswith(suffix)\">3. str.startswith(prefix) and str.endswith(suffix)<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Explanation-3\" title=\"Explanation\">Explanation<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Bonus_Tip\" title=\"Bonus Tip\">Bonus Tip<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#FAQs\" title=\"FAQs\">FAQs<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#How_can_I_check_if_a_string_contains_a_substring_in_Python\" title=\"How can I check if a string contains a substring in Python?\">How can I check if a string contains a substring in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Is_string_containment_case-sensitive_in_Python\" title=\"Is string containment case-sensitive in Python?\">Is string containment case-sensitive in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/metaschool.so\/articles\/python-string-searches\/#Can_I_check_for_multiple_substrings_in_a_single_operation\" title=\"Can I check for multiple substrings in a single operation?\">Can I check for multiple substrings in a single operation?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>Built-in functions have always been a life-saver for developers, all you need to know is what that particular built-in function is used for, or if you are presented with the use case then which function would do the job. Python has a lot of built-in functions and here we are going to talk about the <strong>contains()<\/strong> substring method. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_substring\"><\/span>What is a substring?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before we begin, it is important to know what a substring is. <\/p>\n\n\n\n<p>A <strong>substring<\/strong> is a sequence of characters that appears within another string. It is a contiguous segment of the original string, meaning the characters are adjacent and in the same order. For example, in the string <code>\"hello\"<\/code>, <code>\"ell\"<\/code> and <code>\"lo\"<\/code> are substrings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Does_Python_have_a_string_contains_substring_method\"><\/span>Does Python have a string <code>contains()<\/code> substring method?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.w3schools.com\/java\/ref_string_contains.asp\" target=\"_blank\" rel=\"noopener\"><code>contains()<\/code> function<\/a> in programming is used to check whether a specific substring or pattern is present within a larger string. When you use this function, it returns a boolean value: <code>True<\/code> if the substring is found within the string, and <code>False<\/code> otherwise. The diagram below demonstrates how the <code>contains()<\/code> keyword works.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"301\" src=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/09\/python_contains.drawio.png\" alt=\"contains substring example\" class=\"wp-image-8272\" srcset=\"https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/09\/python_contains.drawio.png 441w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/09\/python_contains.drawio-300x205.png 300w, https:\/\/metaschool.so\/articles\/wp-content\/uploads\/2024\/09\/python_contains.drawio-150x102.png 150w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><figcaption class=\"wp-element-caption\">string <code>contains()<\/code> example<\/figcaption><\/figure>\n<\/div>\n\n\n<p>The <code>contains() <\/code>function has important use cases in text processing tasks, such as searching for keywords, validating input, or filtering data based on content. It helps developers efficiently determine the presence of specific sequences of characters within a string.<\/p>\n\n\n\n<p>Let&#8217;s explore the different methods in Python that can be used to check if a string contains a substring.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Check_for_Substrings_in_Python_Using_%E2%80%9Cin%E2%80%9C\"><\/span>How to Check for Substrings in Python Using &#8220;<code>in<\/code>&#8220;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In Python, you can determine if a string contains a particular substring by using the <code>in<\/code> keyword. Let&#8217;s understand how to implement <code>in<\/code> using a coding example.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"text = &quot;Hello, world!&quot;\nsubstring = &quot;world&quot;\n\nif substring <strong&gt;in<\/strong&gt; text:\n    print(&quot;Substring found!&quot;)\nelse:\n    print(&quot;Substring not found.&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #CE9178\">&quot;Hello, world!&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">substring<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #CE9178\">&quot;world&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">substring<\/span><span style=\"color: #D4D4D4\"> &lt;<\/span><span style=\"color: #9CDCFE\">strong<\/span><span style=\"color: #D4D4D4\">&gt;<\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\">&lt;\/<\/span><span style=\"color: #9CDCFE\">strong<\/span><span style=\"color: #D4D4D4\">&gt; <\/span><span style=\"color: #C8C8C8\">text<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Substring found!&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Substring not found.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, <code>\"world\"<\/code> is checked to see if it is part of the string <code>\"Hello, world!\"<\/code>. In this case, the <code>in<\/code> keyword will return <code>True<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Additional_Methods_for_String_Containment\"><\/span>Additional Methods for String Containment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While the <code>in<\/code> keyword is often sufficient, Python provides additional string methods that can be useful for more specific scenarios. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_strfindsubstring\"><\/span>1. <code>str.find(substring)<\/code><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This method returns the lowest index of the substring if it is found in the string. If the substring is not found, it returns <code>-1<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   index = text.find(substring)\n   if index != -1:\n       print(f&quot;Substring found at index {index}.&quot;)\n   else:\n       print(&quot;Substring not found.&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">find<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">substring<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\"> != -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">f<\/span><span style=\"color: #CE9178\">&quot;Substring found at index {index}.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Substring not found.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explanation\"><\/span>Explanation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Imagine you have a sentence: <strong>&#8220;The quick brown fox jumps over the lazy dog.&#8221;<\/strong> and you want to find the position of the word <strong>&#8220;fox&#8221;<\/strong> within this sentence. If we look through the sentence we find that <strong>&#8220;fox&#8221;<\/strong> starts at the 16th character position. Since the word <strong>&#8220;fox&#8221;<\/strong> is present, we get its starting position, which is 16 (indexing starts at 0). The code then prints <strong>&#8220;Substring found at index 16.&#8221;<\/strong> Instead, if we were searching for the word <strong>&#8220;cat&#8221;<\/strong> the result would be <strong>&#8220;Substring not found.&#8221;<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_strindexsubstring\"><\/span>2. <strong><code>str.index(substring)<\/code><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This method is similar to <code>find()<\/code>, but raises a <code>ValueError<\/code> if the substring is not found. This can be useful if you want to ensure that the substring is present and handle cases where it is not.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   try:\n       index = text.index(substring)\n       print(f&quot;Substring found at index {index}.&quot;)\n   except ValueError:\n       print(&quot;Substring not found.&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #C586C0\">try<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">index<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">substring<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">f<\/span><span style=\"color: #CE9178\">&quot;Substring found at index {index}.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #9CDCFE\">except<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C8C8C8\">ValueError<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Substring not found.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explanation-2\"><\/span>Explanation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Let&#8217;s use the same example <strong>&#8220;The quick brown fox jumps over the lazy dog.&#8221;<\/strong> and we want to find the position of the word <strong>&#8220;quick&#8221;<\/strong>. In this case, this function would behave similar to <code>string.find(substring)<\/code> and print <strong>&#8220;Substring found at index 4.&#8221;<\/strong> If we search for a word like <strong>&#8220;duck&#8221;<\/strong> (which isn&#8217;t in the sentence), we get an error. In this case, the code prints <strong>&#8220;Substring not found.&#8221;<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_strstartswithprefix_and_strendswithsuffix\"><\/span>3. <strong><code>str.startswith(prefix)<\/code><\/strong> and <strong><code>str.endswith(suffix)<\/code><\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>These methods check if a string starts with or ends with a given substring, respectively. They are particularly useful for checking prefixes or suffixes.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"   if text.startswith(&quot;Hello&quot;):\n       print(&quot;String starts with 'Hello'.&quot;)\n   if text.endswith(&quot;world!&quot;):\n       print(&quot;String ends with 'world!'.&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">startswith<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Hello&quot;<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;String starts with &#39;Hello&#39;.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">   <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">endswith<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;world!&quot;<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">       <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;String ends with &#39;world!&#39;.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explanation-3\"><\/span>Explanation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Let&#8217;s use the example <strong>&#8220;Hello, world!&#8221;<\/strong> to explain how the code works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Checking for Start<\/strong>: The code first checks if the string starts with <strong>&#8220;Hello&#8221;<\/strong>. In our example, <strong>&#8220;Hello, world!&#8221;<\/strong> does indeed start with <strong>&#8220;Hello&#8221;<\/strong>, so it prints <strong>&#8220;String starts with &#8216;Hello&#8217;.&#8221;<\/strong><\/li>\n\n\n\n<li><strong>Checking for End<\/strong>: Next, the code checks if the string ends with <strong>&#8220;world!&#8221;<\/strong>. In this example, <strong>&#8220;Hello, world!&#8221;<\/strong> does end with <strong>&#8220;world!&#8221;<\/strong>, so it prints <strong>&#8220;String ends with &#8216;world!'&#8221;<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>If the string did not start with <strong>&#8220;Hello&#8221;<\/strong> or did not end with <strong>&#8220;world!&#8221;<\/strong>, the corresponding messages would not be printed. For instance, if the string were <strong>&#8220;Goodbye, world!&#8221;<\/strong>, the output would be <strong>&#8220;String ends with &#8216;world!&#8221;<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Bonus_Tip\"><\/span>Bonus Tip<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Substring checks are case-sensitive by default. So if you need to perform a case-insensitive check, you can convert both the string and the substring to the same case using <code>str.lower()<\/code> or <code>str.upper()<\/code> (lower case and upper case respectively) and then check if the string contains the substring.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"text = &quot;Hello, World!&quot;\nsubstring = &quot;world&quot;\n\nif substring.lower() in text.lower(): # if &quot;world&quot; in &quot;hello, world&quot;\n    print(&quot;Substring found (case-insensitive).&quot;)\nelse:\n    print(&quot;Substring not found.&quot;)\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #CE9178\">&quot;Hello, World!&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #9CDCFE\">substring<\/span><span style=\"color: #D4D4D4\"> = <\/span><span style=\"color: #CE9178\">&quot;world&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">substring<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">lower<\/span><span style=\"color: #D4D4D4\">() <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">text<\/span><span style=\"color: #D4D4D4\">.<\/span><span style=\"color: #DCDCAA\">lower<\/span><span style=\"color: #D4D4D4\">(): # <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;world&quot;<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #CE9178\">&quot;hello, world&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Substring found (case-insensitive).&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #DCDCAA\">print<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;Substring not found.&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python\u2019s <code>in<\/code> keyword provides a straightforward and effective way to check for substrings within a string in comparison to contains(). For more complex requirements, methods like <code>find()<\/code>, <code>index()<\/code>, <code>startswith()<\/code>, and <code>endswith()<\/code> offer additional functionality. Whether you need a simple string containment check or more advanced substring operations, Python\u2019s string-handling capabilities make it easy to work with textual data.<\/p>\n\n\n\n<p>Python is also crucial in the context of blockchain development. Its versatility and powerful libraries make it a good choice for building and interacting with blockchain systems. To understand why Python is so effective for blockchain projects, check out our article,<a href=\"https:\/\/metaschool.so\/articles\/reasons-why-develop-blockchain-python\/\"> 7 Reasons Why You Should Develop a Blockchain Using Python<\/a> which explores the numerous advantages of using Python for blockchain development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1725958017138\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"How_can_I_check_if_a_string_contains_a_substring_in_Python\"><\/span>How can I check if a string contains a substring in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>In Python, you can check if a string contains a substring using several methods. <br \/>1. Use the <code>in<\/code> keyword, which returns <code>True<\/code> if the substring is present within the string. <br \/>2. Use <code>str.find()<\/code>, which returns the index of the first occurrence of the substring or <code>-1<\/code> if not found.<br \/>3. Use <code>str.index()<\/code>, which raises an error if the substring is not found. <br \/>4. Use <code>startswith()<\/code> and <code>endswith()<\/code> to check if a string begins or ends with a specific substring.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1725958369685\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Is_string_containment_case-sensitive_in_Python\"><\/span>Is string containment case-sensitive in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, string containment checks in Python are case-sensitive by default. This means that the check will distinguish between uppercase and lowercase characters. For instance, checking if <code>\"hello\"<\/code> is in <code>\"Hello World\"<\/code> will return <code>False<\/code> because of the case difference. To perform a case-insensitive check, you should convert both the main string and the substring to the same case using <code>str.lower()<\/code> or <code>str.upper()<\/code>.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1725958405844\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Can_I_check_for_multiple_substrings_in_a_single_operation\"><\/span>Can I check for multiple substrings in a single operation?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Python does not provide a built-in method to check for multiple substrings in a single operation. However, you can achieve this by iterating through a list of substrings and checking each one individually. For example, you can use a loop with the <code>in<\/code> keyword to check if any of the substrings are present in the main string.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":19,"featured_media":10936,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[342],"tags":[],"class_list":["post-8262","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/8262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=8262"}],"version-history":[{"count":17,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/8262\/revisions"}],"predecessor-version":[{"id":12289,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/8262\/revisions\/12289"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/10936"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=8262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=8262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=8262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}