{"id":1276,"date":"2019-12-26T03:26:25","date_gmt":"2019-12-26T03:26:25","guid":{"rendered":"https:\/\/www.askpython.com\/?p=1276"},"modified":"2022-08-06T13:17:10","modified_gmt":"2022-08-06T13:17:10","slug":"python-string-substring","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-string-substring","title":{"rendered":"Python String Substring"},"content":{"rendered":"\n<p>Substring is a portion of the string that can be extracted and represented as a new string. In this tutorial, we will learn how to work with Substrings in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Creating Python String Substring<\/h2>\n\n\n\n<p>Substring can be created using either of the following methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>Slicing<\/strong><\/code><\/li><li><code><strong>split()<\/strong><\/code> method<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput = &#039;Engineering Field&#039;\n\n#substring using slice\nresult = input&#x5B;5:]\nprint(result)\n\n#substring using split\nresult1 = input.split()\nprint(result1)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"507\" height=\"124\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Creation-Substring.png\" alt=\"Creation-Substring\" class=\"wp-image-1469\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Creation-Substring.png 507w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Creation-Substring-300x73.png 300w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><figcaption><em>Creation-Substring<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"175\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring_creation-Example-1024x175.png\" alt=\"Substring Creation Example\" class=\"wp-image-1472\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring_creation-Example-1024x175.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring_creation-Example-300x51.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring_creation-Example-768x131.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring_creation-Example.png 1159w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>Substring Creation Example<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Check for the presence of a substring in a String<\/h2>\n\n\n\n<p>The presence of sub-string in a string can be checked by either of the following methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>By using <code><strong>in<\/strong><\/code> operator<\/li><li>By using <code><strong>find()<\/strong><\/code> method<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput = &#039;Medical Colleges in India.&#039;\n#using in operator\nif &#039;India&#039; in input:\n    print(&#039;Substring is present in the input.&#039;)\n#using find() method\nif input.find(&#039;Medical Colleges&#039;) != -1:\n    print(&#039;Substring is present in the input.&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p><code>Substring is present in the input.<br>Substring is present in the input.<\/code><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"918\" height=\"169\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring-presence-example.png\" alt=\"Substring Presence Example\" class=\"wp-image-1473\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring-presence-example.png 918w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring-presence-example-300x55.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Substring-presence-example-768x141.png 768w\" sizes=\"auto, (max-width: 918px) 100vw, 918px\" \/><figcaption><em>Substring Presence Example<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Get the substring from a String in Python<\/h2>\n\n\n\n<p>The following are some of the methods to get the substring from a given string:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>By using <code><strong>list slicing<\/strong><\/code><\/li><li>By using <code><strong>itertools.combinations()<\/strong><\/code> method<\/li><\/ul>\n\n\n\n<p><strong>Example 1: <\/strong><em>By using List Slicing<\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstr = &#039;Engineering Discipline&#039;\n  \nsub1 = str&#x5B;:3] \nsub2 = str&#x5B;6:] \n  \n\nprint (&quot;Resultant substring from the start: &quot;, sub1) \nprint (&quot;Resultant substring from the end: &quot;, sub2) \n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"125\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring.png\" alt=\"Output Fetch Substring\" class=\"wp-image-1475\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring.png 714w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring-300x53.png 300w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><figcaption><em>Output-Fetch Substring<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Example 2<\/strong>: <em>By using itertools.combinations() method <\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom itertools import combinations \n  \n\nstr1 = &quot;Safa&quot;\n  \n\nprint(&quot;The original string is : &quot; + str(str1)) \n  \n \nresult = &#x5B;str1&#x5B;a:b] for a, b in combinations( \n            range(len(str1) + 1), r = 2)] \n  \n\nprint(&quot;All substrings of string are : &quot; + str(result)) \n\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"124\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring-1-1024x124.png\" alt=\"Output Fetch Substring 1\" class=\"wp-image-1478\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring-1-1024x124.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring-1-300x36.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring-1-768x93.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-fetch-substring-1.png 1031w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption><em>Output-Fetch Substring 1<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Count of occurrence of a substring in a String<\/h2>\n\n\n\n<p>The count() function is used to represent the count of the characters\/sub-string in the given string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring = &#039;Engineering Discipline&#039;\n\nprint(&#039;Count of Sub-string =&#039;, string.count(&#039;i&#039;))\n\nstring1 = &#039;Aroma Arena&#039;\nprint(&#039;Count of Sub-string =&#039;, string1.count(&#039;Ar&#039;))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-count-substring.png\" alt=\"Output Count Substring\" class=\"wp-image-1485\" width=\"707\" height=\"153\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-count-substring.png 578w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Output-count-substring-300x65.png 300w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><figcaption><em>Output Count Substring<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"64\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Count-substring.png\" alt=\"Count Substring\" class=\"wp-image-1487\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Count-substring.png 726w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/12\/Count-substring-300x26.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><figcaption><em>Count Substring<\/em><\/figcaption><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Get the indexes of all substring<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef find_index(str1, substring):\n    l2 = &#x5B;]\n    length = len(str1)\n    index = 0\n    while index &lt; length:\n        x = str1.find(substring, index)\n        if x == -1:\n            return l2\n        l2.append(x)\n        index = x + 1\n    return l2\n\n\nstring = &#039;Aroma Arena at the desert Arone.&#039;\nprint(find_index(string, &#039;Ar&#039;))\n<\/pre><\/div>\n\n\n<p>The above snippet of code gives us the index\/position of all of the occurrence of a particular substring in a string.<\/p>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p>[0, 6, 26]<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python substring<\/li><li><a href=\"https:\/\/docs.python.org\/3.8\/library\/string.html\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Python string functions Documentation<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Substring is a portion of the string that can be extracted and represented as a new string. In this tutorial, we will learn how to work with Substrings in Python. 1. Creating Python String Substring Substring can be created using either of the following methods: Slicing split() method Output: 2. Check for the presence of [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1276","post","type-post","status-publish","format-standard","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1276","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=1276"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/1276\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=1276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=1276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=1276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}