{"id":2570,"date":"2020-02-01T19:01:08","date_gmt":"2020-02-01T13:31:08","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=2570"},"modified":"2023-12-30T15:41:50","modified_gmt":"2023-12-30T10:11:50","slug":"python-substring","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-substring\/","title":{"rendered":"Python Substring | Substring Operations in Python"},"content":{"rendered":"\n<p><strong>Python substring<\/strong> is a string that is part of another (or larger) <strong>Python String<\/strong>. In Python Programming, this method is also known as the <strong>slicing of string<\/strong>.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#How_to_Create_a_Python_Substring\" >How to Create a Python 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:\/\/www.pythonpool.com\/python-substring\/#Creating_Python_Substring_Using_Slice_Method\" >Creating Python Substring Using Slice 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:\/\/www.pythonpool.com\/python-substring\/#Creating_Python_Substring_Using_Split_Method\" >Creating Python Substring Using Split Method<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Python_String_Methods\" >Python String Methods<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Table_Containing_all_Python_String_Methods\" >Table Containing all Python String Methods<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Extracting_Substring_in_Python\" >Extracting Substring in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#How_to_check_if_a_string_contains_a_substring_in_Python\" >How to check if a string contains a substring in Python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#1_Python_Substring_using_the_find_method\" >1. Python Substring using the find method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#2_Using_the_in_operator_to_find_Python_Substring\" >2. Using the in&nbsp;operator to find Python Substring<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#3_By_using_count_method\" >3. By using count() method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Example_Checking_for_the_presence_of_substring_within_a_string_using_count_method\" >Example: Checking for the presence of substring within a string using count() method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#4_Using_Contains_Method\" >4. Using Contains Method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#5_Using_Regular_Expressions_REGEX_to_Find_Python_Substring\" >5. Using Regular Expressions (REGEX) to Find Python Substring<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Example_of_Alternate_Characters_Substrings_in_Python\" >Example of Alternate Characters Substrings in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Python_Substring_using_For_Loop\" >Python Substring using For Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Python_substring_match\" >Python substring match<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Python_Program_to_Find_all_indexes_of_substring\" >Python Program to Find all indexes of substring<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Summary\" >Summary:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#Trending_Python_Articles\" >Trending Python Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Create_a_Python_Substring\"><\/span>How to Create a Python Substring<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are generally two methods of creating substrings in Python Programming Language.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Slice<\/li>\n\n\n\n<li>Split<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Python_Substring_Using_Slice_Method\"><\/span>Creating Python Substring Using Slice Method<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, you have to store a string in a Python variable.&nbsp;Our example is: <br><strong>str = &#8220;Hello world this is Karan from Python Pool&#8221;<\/strong><\/li>\n\n\n\n<li>With the command <strong>&#8220;str [4:]&#8221;<\/strong> you will now output the string without the first four characters:<br><strong> &#8216;o world this is Karan from Python Pool&#8217;<\/strong><\/li>\n\n\n\n<li>With the command <strong>&#8220;str [: 4]&#8221;<\/strong> in turn, only the first four characters are output: <br><strong>&#8220;Hell&#8221;<\/strong><\/li>\n\n\n\n<li>The command <strong>&#8220;str [: &#8211; 2]&#8221;<\/strong>, which outputs the string without the last two characters, is also very practical: <br>&#8220;<strong>Hello world this is Karan from Python Po&#8221;<\/strong> <\/li>\n\n\n\n<li>This also works the other way round, <strong>&#8220;str[-2:]&#8221;<\/strong> so that only the last two characters are output:<br><strong>&#8220;ol&#8221;<\/strong><\/li>\n\n\n\n<li>Finally, you can also combine the commands.&nbsp;For example, the command <strong>&#8220;x [4: -4]&#8221;<\/strong> outputs the string without the first and last four characters:<br><strong>&#8216;o world this is Karan from Python &#8216;<\/strong> <\/li>\n<\/ul>\n\n\n\n<p><em>You can Execute all the above commands in the Python terminal, like the picture shown below.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"670\" height=\"327\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-12.jpg\" alt=\"Python Terminal\" class=\"wp-image-2575\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-12.jpg 670w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/01\/Capture-12-300x146.jpg 300w\" sizes=\"(max-width: 670px) 100vw, 670px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Python_Substring_Using_Split_Method\"><\/span>Creating Python Substring Using Split Method<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Split strings is another function that can be applied in Python let see for the string &#8220;Python Pool Best Place to Learn Python&#8221;. First here we will split the string by using the command word. split and get the result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">word=\"Python Pool Best Place to Learn Python\" \nprint(word.split(' '))<\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Python', 'Pool', 'Best', 'Place', 'to', 'Learn', 'Python']<\/code><\/pre>\n\n\n\n<p>To understand this better we will see one more example of split, instead of space (&#8216; &#8216;) we will replace it with (&#8216;r&#8217;) and it will split the string wherever &#8216;r&#8217; is mentioned in the string<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">word=\"Python Pool Best Place to Learn Python \" \nprint(word.split('r'))<\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Python Pool Best Place to Lea', 'n Python ']<\/code><\/pre>\n\n\n\n<p><strong>Note: <\/strong>In Python, Strings are immutable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"methods\"><span class=\"ez-toc-section\" id=\"Python_String_Methods\"><\/span>Python String Methods<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A method in Python is similar to a function, but it runs &#8220;on&#8221; an object. If the variable s is considered as a string, then the code s.lower() runs the lower() method on that string object and then returns the result (this concept of a method running on an object is one of the basic ideas that make up Object-Oriented Programming, OOP)<\/p>\n\n\n\n<p>Python substring has quite a few methods that string objects can call in order to perform frequently occurring tasks (related to string). For example, if you require the first letter of a string to be capitalized, you can use capitalize() method. Below are all methods of string objects. Also, all built-in functions that can take a string as a parameter and perform some task are included.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Table_Containing_all_Python_String_Methods\"><\/span>Table Containing all Python String Methods<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-table alignfull\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Method<\/strong><\/th><th><strong>Description<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Python String capitalize()<\/td><td>Converts first character to Capital Letter<\/td><\/tr><tr><td>Python String center()<\/td><td>Pads string with the specified character<\/td><\/tr><tr><td>Python String casefold()<\/td><td>converts to case folded strings<\/td><\/tr><tr><td>Python String count()<\/td><td>returns occurrences of a substring<\/td><\/tr><tr><td>Python String endswith()<\/td><td>Checks if String Ends with the Specified Suffix<\/td><\/tr><tr><td>Python String expandtabs()<\/td><td>Replaces Tab With Spaces<\/td><\/tr><tr><td>Python String encode()<\/td><td>returns encoded string<\/td><\/tr><tr><td>Python String find()<\/td><td>Returns the index of the first occurrence of a substring<\/td><\/tr><tr><td>Python String format()<\/td><td>formats string<\/td><\/tr><tr><td>Python String index()<\/td><td>Returns Index of the Python Substring<\/td><\/tr><tr><td>Python String isalnum()<\/td><td>Checks Alphanumeric<\/td><\/tr><tr><td>Python String isalpha()<\/td><td>Checks if All are Alphabets<\/td><\/tr><tr><td>Python String isdecimal()<\/td><td>Checks Decimals<\/td><\/tr><tr><td>Python String isdigit()<\/td><td>Checks Digits<\/td><\/tr><tr><td>Python String isidentifier()<\/td><td>Checks for Valid Identifier<\/td><\/tr><tr><td>Python String islower()<\/td><td>Checks if all are Lowercase<\/td><\/tr><tr><td>Python String isnumeric()<\/td><td>Checks Numeric<\/td><\/tr><tr><td>Python String isprintable()<\/td><td>Checks Printable<\/td><\/tr><tr><td>Python String isspace()<\/td><td>Checks Whitespace<\/td><\/tr><tr><td>Python String istitle()<\/td><td>Checks for Titlecased<\/td><\/tr><tr><td>Python String isupper()<\/td><td>returns if all are uppercase<\/td><\/tr><tr><td>Python String join()<\/td><td>Returns Concatenated String<\/td><\/tr><tr><td>Python String ljust()<\/td><td>returns left-justified string<\/td><\/tr><tr><td>Python String rjust()<\/td><td>returns right-justified string<\/td><\/tr><tr><td>Python String lower()<\/td><td>returns lowercased string<\/td><\/tr><tr><td>Python String upper()<\/td><td>returns uppercased string<\/td><\/tr><tr><td>Python String swapcase()<\/td><td>swap uppercase to lowercase<\/td><\/tr><tr><td>Python String lstrip()<\/td><td>Removes Leading<\/td><\/tr><tr><td>Python String rstrip()<\/td><td>Removes Trailing<\/td><\/tr><tr><td>Python String strip()<\/td><td>Removes Both Leading and Trailing<\/td><\/tr><tr><td>Python String partition()<\/td><td>Returns a Tuple<\/td><\/tr><tr><td>Python String maketrans()<\/td><td>returns a translation table<\/td><\/tr><tr><td>Python String rpartition()<\/td><td>Returns a Tuple<\/td><\/tr><tr><td>Python String translate()<\/td><td>returns mapped string<\/td><\/tr><tr><td>Python String replace()<\/td><td>Replaces Substring Inside<\/td><\/tr><tr><td>Python String rfind()<\/td><td>Returns the Highest Index of the Substring<\/td><\/tr><tr><td>Python String split()<\/td><td>Splits String from Left<\/td><\/tr><tr><td>Python String rsplit()<\/td><td>Splits String From Right<\/td><\/tr><tr><td>Python String startswith() <\/td><td> Checks if String Starts with the Specified String <\/td><\/tr><tr><td>Python String title()<\/td><td>Returns a Title Cased String<\/td><\/tr><tr><td>Python String zfill()<\/td><td>Returns a copy of String Padded With Zeros<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">String Methods<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-substring\"><span class=\"ez-toc-section\" id=\"Extracting_Substring_in_Python\"><\/span>Extracting Substring in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can extract substrings in Python with square brackets that may contain one or two indices and a colon. Like so,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>myString[0]<\/code>&nbsp;extracts the&nbsp;<strong>first<\/strong>&nbsp;character;<\/li>\n\n\n\n<li><code>myString[1:]<\/code>&nbsp;the&nbsp;<strong>second through last<\/strong>&nbsp;characters;<\/li>\n\n\n\n<li><code>myString[:4]<\/code>&nbsp;extracts the&nbsp;<strong>first through fourth<\/strong>&nbsp;characters;<\/li>\n\n\n\n<li><code>myString[1:3]<\/code>&nbsp;the&nbsp;<strong>second through third<\/strong>&nbsp;characters;<\/li>\n\n\n\n<li><code>myString[-1]<\/code>&nbsp;extracts the&nbsp;<strong>last<\/strong>&nbsp;character.<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-orange-color has-text-color has-medium-font-size\"><strong>Must Read<\/strong>: <a href=\"http:\/\/www.pythonpool.com\/python-book\/\">Python Book | Best Book to Learn Python<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_check_if_a_string_contains_a_substring_in_Python\"><\/span>How to check if a string contains a substring in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>No matter whether it\u2019s just a word, a letter or a phrase that you want to check in a string, with Python you can easily utilize the built-in methods and the membership test&nbsp;<strong>in<\/strong>&nbsp;operator.<\/p>\n\n\n\n<p>It is worth noting that you will get a&nbsp;<strong>boolean value<\/strong>&nbsp;(True or False) or an&nbsp;<strong>integer<\/strong>&nbsp;to indicate if the string contains what you searched for. You\u2019ll know more about it as I show the code below.<\/p>\n\n\n\n<p>Let us take a look at the potential solutions with the help of which you can find if a string or substring in Python contains a specific word\/letter.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>By using<code>&nbsp;find()<\/code>&nbsp;method<\/li>\n\n\n\n<li>Using&nbsp;<code>in<\/code>&nbsp;operator<\/li>\n\n\n\n<li>By using&nbsp;<code>count()<\/code>&nbsp;method<\/li>\n\n\n\n<li>Using&nbsp;<code>operator.contains()<\/code>&nbsp;method<\/li>\n\n\n\n<li>By using Regular Expressions (REGEX)<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Python_Substring_using_the_find_method\"><\/span>1. Python Substring using the find method <span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another method you can use is the&nbsp;string\u2019s find method.<\/p>\n\n\n\n<p>Unlike the in operator which is evaluated to a boolean value, the&nbsp;find&nbsp;method returns an integer.<\/p>\n\n\n\n<p>This integer is essentially the index of the beginning of the substring if the substring exists, otherwise, -1 is returned.<\/p>\n\n\n\n<p>Let\u2019s see the&nbsp;find&nbsp;method in action.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; str = \"Messi is the best soccer player\"\n&gt;&gt;&gt; str.find(\"soccer\")\n18\n&gt;&gt;&gt; str.find(\"Ronaldo\")\n-1\n&gt;&gt;&gt; str.find(\"Messi\")\n0<\/code><\/pre>\n\n\n\n<p>One cool thing about this method is you can optionally specify a start index and an end index to limit your search within.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_the_in_operator_to_find_Python_Substring\"><\/span>2. Using the in&nbsp;operator to find Python Substring<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <strong>in<\/strong>&nbsp;operator returns true if the substring exists in a string and false if \u200botherwise.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The general syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>substring&nbsp;in&nbsp;string<\/code><\/pre>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a_string=\"Python Programming\"\nsubstring1=\"Programming\"\nsubstring2=\"Language\"\nprint(\"Check if \"+a_string+\" contains \"+substring1+\":\")\nprint(substring1 in a_string)\nprint(\"Check if \"+a_string+\" contains \"+substring2+\":\")\nprint(substring2 in a_string)<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Check if Python Programming contains Programming:\nTrue\nCheck if Python Programming contains Language:\nFalse<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_By_using_count_method\"><\/span>3. By using count() method<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The count() for Finding or searching Python substring 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>: string.count(substring)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_Checking_for_the_presence_of_substring_within_a_string_using_count_method\"><\/span>Example: Checking for the presence of substring within a string using count() method<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>str=\"Python Pool is the Best Place to Learn Python\"\nsub1=\"Python\"\nsub2=\"Pool\"\nsub3=\"Done\"\nprint(str.count(sub1)) \n \nprint(str.count(sub2))\n \nprint(str.count(sub3))<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2\n1\n0<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_Contains_Method\"><\/span>4. Using Contains Method<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>__contains__()<\/strong>&nbsp;is another function to help you to check whether a string contains a particular letter\/word.<\/p>\n\n\n\n<p>Here\u2019s how you can use it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stringexample = \"kiki\"\nstringexample.__contains__(\"k\")<\/code><\/pre>\n\n\n\n<p>You will get the output as&nbsp;<strong>True\/False<\/strong>. For the above code snippet, you will get the output as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n\n\n\n<p><em>Do note that there are 4 underscore symbols involved when you want to write the method (2 before the word and 2 after).<\/em><\/p>\n\n\n\n<p>Here\u2019s a program to explain the same:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>stringexample = \"kiki\"\nif stringexample.__contains__(\"k\") == True:\n  print (\"Yeyy, found the substring!\")\nelse:\n  print (\"Oops, not found!\")<\/code><\/pre>\n\n\n\n<p>In this case, the output is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Yeyy, found the substring!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4regularexpressionsregex\"><span class=\"ez-toc-section\" id=\"5_Using_Regular_Expressions_REGEX_to_Find_Python_Substring\"><\/span>5. Using Regular Expressions (REGEX) to Find Python Substring<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Regular expressions provide a more flexible (albeit more complex) way to check python substrings for pattern matching. Python is shipped with a built-in module for regular expressions, called&nbsp;<code>re<\/code>. The&nbsp;<code>re<\/code>&nbsp;module contains a function called&nbsp;<code>search<\/code>, which we can use to match a substring pattern as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from re import search\n\nfullstring = \"StackAbuse\"\nsubstring = \"tack\"\n\nif search(substring, fullstring):\n    print \"Found!\"\nelse:\n    print \"Not found!\"<\/code><\/pre>\n\n\n\n<p>This method is best if you are needing a more complex matching function, like case insensitive matching. Otherwise, the complication and slower speed of regex should be avoided for simple substring matching use-cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_of_Alternate_Characters_Substrings_in_Python\"><\/span>Example of Alternate Characters Substrings in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use the same slicing concept in python to generate substrings by forming a lot more logics. The following lines of code will help you form a string by choosing the alternate string characters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fullString = \"Alternate Characters\"\n#Choose Alternate Characters &amp; Forms the SubString\nsubString = fullString&#91;::2] #Basically we're selecing every 2nd chracter\nprint(subString)<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AtraeCaatr<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_Substring_using_For_Loop\"><\/span>Python Substring using For Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use&nbsp;<strong>for loop<\/strong>&nbsp;with&nbsp;<strong>range function<\/strong>&nbsp;to return a substring.&nbsp; For this, we have to use&nbsp;<strong>print function<\/strong>&nbsp;along with end argument. This Python string example returns a substring starts at 3 and ends at 24.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">string = 'Python Programming Language for Free'\n \nfor n in range(3, 25):\n &nbsp; &nbsp;print(string[n], end = '')<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_substring_match\"><\/span>Python substring match<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this example, we check whether the substring is present in the given string or not using the&nbsp;<strong>Python If Else<\/strong>&nbsp;and&nbsp;<strong>not In operator<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">string = 'Hello World'\n \nsubstring = 'Hello'\n \nif substring not in string:\n &nbsp; &nbsp;print('We haven\\'t found what you are looking for = ', substring)\nelse:\n &nbsp; &nbsp;print('We found the Matching substring = ', substring)<\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>We found the Matching substring =  Hello<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python_Program_to_Find_all_indexes_of_substring\"><\/span>Python Program to Find all indexes of substring<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There is no built-in function to get the list of all the indexes for the substring. However, we can easily define one using find() function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ndef find_all_indexes(input_str, substring):\n    l2 = &#91;]\n    length = len(input_str)\n    index = 0\n    while index &lt; length:\n        i = input_str.find(substring, index)\n        if i == -1:\n            return l2\n        l2.append(i)\n        index = i + 1\n    return l2\n\n\ns = 'This Is The Best Theorem'\nprint(find_all_indexes(s, 'Th'))<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;0, 8, 17]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Since Python is an object-oriented programming language, many functions can be applied to Python objects and <strong>Python Substring<\/strong>. A notable feature of Python is its indenting source statements to make the code easier to read.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Accessing values through slicing &#8211; square brackets are used for slicing along with the index or indices to obtain a substring.\n<ul class=\"wp-block-list\">\n<li>In slicing, if the range is declared [1:5], it can actually fetch the value from the range [1:4]<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>You can update Python String by re-assigning a variable to another string<\/li>\n\n\n\n<li>Method replace() returns a copy of the string in which the occurrence of old is replaced with new.\n<ul class=\"wp-block-list\">\n<li>The syntax for method replaces oldstring.replace(&#8220;value to change&#8221;,&#8221;value to be replaced&#8221;)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>String operators like [], [: ], in, Not in, etc. can be applied to concatenate the string, fetching or inserting specific characters into the string, or to check whether certain character exist in the string<\/li>\n<\/ul>\n\n\n\n<p>With slices or <strong>Python Substring<\/strong>, we extract parts of strings. We can specify an optional start index and an optional last index (not a length). Offsets are useful. <\/p>\n\n\n\n<p>If you still have any doubt or confusion, do let us know in the comment section below.<\/p>\n\n\n\n<p><em>Happy Coding!<\/em><\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Python_Articles\"><\/span>Trending Python Articles<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the slicing &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Substring | Substring Operations in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-substring\/#more-2570\" aria-label=\"More on Python Substring | Substring Operations in Python\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2578,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[369,366,382,381,386,364,378,374,392,380,359,388,372,394,391,365,373,360,363,370,368,383,390,375,371,377,387,362,376,361,384,393,367,379,389,385],"class_list":["post-2570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-check-if-a-string-contains-a-substring-python","tag-check-if-string-contains-substring-python","tag-check-if-substring-in-string-python","tag-find-a-substring-in-a-string-python","tag-find-all-occurrences-of-a-substring-in-a-string-python","tag-find-substring-in-string-python","tag-find-substring-python","tag-get-substring-python","tag-longest-common-substring-python","tag-longest-palindromic-substring-python","tag-longest-substring-without-repeating-characters-python","tag-python-check-for-substring","tag-python-check-if-string-contains-substring","tag-python-check-if-string-contains-substring-from-list","tag-python-check-if-substring-in-string","tag-python-extract-substring","tag-python-find-substring","tag-python-find-substring-in-string","tag-python-get-substring","tag-python-remove-substring","tag-python-remove-substring-from-string","tag-python-replace-substring","tag-python-search-string-for-substring","tag-python-string-contains-substring","tag-python-string-find-substring","tag-python-string-remove-substring","tag-python-string-substring","tag-python-substring","tag-python-substring-function","tag-python-substring-in-string","tag-python-substring-index","tag-python-substring-match","tag-remove-substring-from-string-python","tag-replace-substring-python","tag-substring-in-python","tag-substring-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Substring | Substring Operations in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-substring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Substring | Substring Operations in Python\" \/>\n<meta property=\"og:description\" content=\"Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the slicing of string.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-substring\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-02-01T13:31:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-30T10:11:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1019\" \/>\n\t<meta property=\"og:image:height\" content=\"614\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Python Pool\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Python Pool\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/\"},\"author\":{\"name\":\"Python Pool\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\"},\"headline\":\"Python Substring | Substring Operations in Python\",\"datePublished\":\"2020-02-01T13:31:08+00:00\",\"dateModified\":\"2023-12-30T10:11:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/\"},\"wordCount\":1654,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg\",\"keywords\":[\"check if a string contains a substring python\",\"check if string contains substring python\",\"check if substring in string python\",\"find a substring in a string python\",\"find all occurrences of a substring in a string python\",\"find substring in string python\",\"find substring python\",\"get substring python\",\"longest common substring python\",\"longest palindromic substring python\",\"longest substring without repeating characters python\",\"python check for substring\",\"python check if string contains substring\",\"python check if string contains substring from list\",\"python check if substring in string\",\"python extract substring\",\"python find substring\",\"python find substring in string\",\"python get substring\",\"python remove substring\",\"python remove substring from string\",\"python replace substring\",\"python search string for substring\",\"python string contains substring\",\"python string find substring\",\"python string remove substring\",\"python string substring\",\"python substring\",\"python substring function\",\"python substring in string\",\"python substring index\",\"python substring match\",\"remove substring from string python\",\"replace substring python\",\"substring in python\",\"substring python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-substring\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-substring\/\",\"name\":\"Python Substring | Substring Operations in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg\",\"datePublished\":\"2020-02-01T13:31:08+00:00\",\"dateModified\":\"2023-12-30T10:11:50+00:00\",\"description\":\"Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the...\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-substring\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg\",\"width\":1019,\"height\":614,\"caption\":\"python substring\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-substring\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Substring | Substring Operations in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\",\"name\":\"Python Pool\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"caption\":\"Python Pool\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Substring | Substring Operations in Python - Python Pool","description":"Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-substring\/","og_locale":"en_US","og_type":"article","og_title":"Python Substring | Substring Operations in Python","og_description":"Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the slicing of string.","og_url":"https:\/\/www.pythonpool.com\/python-substring\/","og_site_name":"Python Pool","article_published_time":"2020-02-01T13:31:08+00:00","article_modified_time":"2023-12-30T10:11:50+00:00","og_image":[{"width":1019,"height":614,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg","type":"image\/jpeg"}],"author":"Python Pool","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Python Pool","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-substring\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-substring\/"},"author":{"name":"Python Pool","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998"},"headline":"Python Substring | Substring Operations in Python","datePublished":"2020-02-01T13:31:08+00:00","dateModified":"2023-12-30T10:11:50+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-substring\/"},"wordCount":1654,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg","keywords":["check if a string contains a substring python","check if string contains substring python","check if substring in string python","find a substring in a string python","find all occurrences of a substring in a string python","find substring in string python","find substring python","get substring python","longest common substring python","longest palindromic substring python","longest substring without repeating characters python","python check for substring","python check if string contains substring","python check if string contains substring from list","python check if substring in string","python extract substring","python find substring","python find substring in string","python get substring","python remove substring","python remove substring from string","python replace substring","python search string for substring","python string contains substring","python string find substring","python string remove substring","python string substring","python substring","python substring function","python substring in string","python substring index","python substring match","remove substring from string python","replace substring python","substring in python","substring python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-substring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-substring\/","url":"https:\/\/www.pythonpool.com\/python-substring\/","name":"Python Substring | Substring Operations in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg","datePublished":"2020-02-01T13:31:08+00:00","dateModified":"2023-12-30T10:11:50+00:00","description":"Python substring is a string that is part of another (or larger) Python String. In Python Programming, this method is also known as the...","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-substring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-substring\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-substring\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/02\/Capture.jpg","width":1019,"height":614,"caption":"python substring"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-substring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python Substring | Substring Operations in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998","name":"Python Pool","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","caption":"Python Pool"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/2570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=2570"}],"version-history":[{"count":13,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/2570\/revisions"}],"predecessor-version":[{"id":31315,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/2570\/revisions\/31315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/2578"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=2570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=2570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=2570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}