{"id":72935,"date":"2022-06-20T16:51:11","date_gmt":"2022-06-20T11:21:11","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/"},"modified":"2025-06-12T04:16:43","modified_gmt":"2025-06-11T22:46:43","slug":"python-substring","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/","title":{"rendered":"What is a Python Substring?"},"content":{"rendered":"\n<p>A Python <strong>substring<\/strong> is a part of a larger string. For example, \"py\" is a substring of \"python\". Knowing how to get and work with substrings is very useful for handling text data.<\/p>\n\n\n\n<p>It helps you extract specific parts of text. You can use it to get names from a full address, or extract specific data from a log file. This saves time and makes text processing easy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-python-substring\">What is a Python Substring?<\/h2>\n\n\n\n<p>A Python substring is a sequence of characters within another string. You do not create a separate data type for substrings. They are just regular strings that you extract from a larger one.<\/p>\n\n\n\n<p>Python uses a technique called <strong>string slicing<\/strong> to get substrings. Slicing lets you pick a part of a string by defining a start and an end point.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-get-substrings-string-slicing\">How to Get Substrings (String Slicing)<\/h2>\n\n\n\n<p>You use square brackets <code>[]<\/code> with slice notation to get a substring. The general format is <code>string[start:end:step]<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>start<\/code>: The index where the substring begins. This index is included. (Default is <code>0<\/code> if not specified).<\/li>\n\n\n\n<li><code>end<\/code>: The index where the substring ends. This index is <strong>not<\/strong> included. (Default is the end of the string if not specified).<\/li>\n\n\n\n<li><code>step<\/code>: How many characters to jump. (Default is <code>1<\/code> if not specified).<\/li>\n<\/ul>\n\n\n\n<p>Python uses zero-based indexing. This means the first character is at index 0, the second at 1, and so on.<\/p>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-title-link\">Python Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>11.5 Hrs<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>51 Coding Exercises<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/master-python-programming\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p>Let's look at examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-basic-slicing-stringstartend\">1. Basic Slicing: <code>string[start:end]<\/code><\/h3>\n\n\n\n<p>This is the most common way to get a substring. You specify a starting index and an ending index.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;hello world&quot;\n\n# Get characters from index 0 up to (but not including) index 5\nsub1 = my_string&#x5B;0:5]\nprint(f&quot;Substring 1: &#039;{sub1}&#039;&quot;) \n\n# Get characters from index 6 up to (but not including) index 11\nsub2 = my_string&#x5B;6:11]\nprint(f&quot;Substring 2: &#039;{sub2}&#039;&quot;) \n\n# Get a single character (from index 4 to 5)\nsub3 = my_string&#x5B;4:5]\nprint(f&quot;Substring 3: &#039;{sub3}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSubstring 1: &#039;hello&#039;\nSubstring 2: &#039;world&#039;\nSubstring 3: &#039;o&#039;\n<\/pre><\/div>\n\n\n<p>Remember, the end index is exclusive. The character at the end index is not part of the result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-slicing-to-the-end-stringstart\">2. Slicing to the End: <code>string[start:]<\/code><\/h3>\n\n\n\n<p>If you omit the end index, Python extracts characters from the start index to the end of the string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;python programming&quot;\n\n# Get substring from index 7 to the end\nsub = my_string&#x5B;7:]\nprint(f&quot;Substring: &#039;{sub}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSubstring: &#039;programming&#039;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"3-slicing-from-the-beginning-stringend\">3. Slicing from the Beginning: <code>string[:end]<\/code><\/h3>\n\n\n\n<p>If you omit the start index, Python extracts characters from the beginning of the string up to (but not including) the end index.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;python programming&quot;\n\n# Get substring from the beginning up to (but not including) index 6\nsub = my_string&#x5B;:6]\nprint(f&quot;Substring: &#039;{sub}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSubstring: &#039;python&#039;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"4-full-copy-string\">4. Full Copy: <code>string[:]<\/code><\/h3>\n\n\n\n<p>If you omit both start and end indices, you get a full copy of the original string. This is useful when you want to make a copy without modifying the original.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;data science&quot;\n\n# Get a full copy\nsub = my_string&#x5B;:]\nprint(f&quot;Substring: &#039;{sub}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nSubstring: &#039;data science&#039;\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"5-using-negative-indices\">5. Using Negative Indices<\/h3>\n\n\n\n<p>Negative indices count from the end of the string. The last character is at index -1, the second to last at -2, and so on.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;example&quot;\n\n# Get the last character\nlast_char = my_string&#x5B;-1]\nprint(f&quot;Last character: &#039;{last_char}&#039;&quot;) \n\n# Get the last three characters\nlast_three = my_string&#x5B;-3:]\nprint(f&quot;Last three: &#039;{last_three}&#039;&quot;) \n\n# Get characters from index -5 up to (but not including) -2\npart = my_string&#x5B;-5:-2]\nprint(f&quot;Part: &#039;{part}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nLast character: &#039;e&#039;\nLast three: &#039;ple&#039;\nPart: &#039;amp&#039;\n<\/pre><\/div>\n\n\n<p>Negative indices are useful for extracting parts from the end of a string without knowing its exact length.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-slicing-with-a-step-stringstartendstep\">6. Slicing with a Step: <code>string[start:end:step]<\/code><\/h3>\n\n\n\n<p>The <code>step<\/code> argument lets you skip characters. For example, a step of 2 means you take every second character.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;abcdefgh&quot;\n\n# Get every second character from the beginning to the end\nevery_second = my_string&#x5B;::2]\nprint(f&quot;Every second char: &#039;{every_second}&#039;&quot;) \n\n# Get characters from index 1 to 6, with a step of 2\nsub_stepped = my_string&#x5B;1:7:2]\nprint(f&quot;Stepped substring: &#039;{sub_stepped}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nEvery second char: &#039;aceg&#039;\nStepped substring: &#039;bdf&#039;\n<\/pre><\/div>\n\n\n<p>A common trick is using a negative step to reverse a string:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmy_string = &quot;reverse&quot;\n\n# Reverse the string\nreversed_str = my_string&#x5B;::-1]\nprint(f&quot;Reversed string: &#039;{reversed_str}&#039;&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nReversed string: &#039;esrever&#039;\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"useful-string-methods-for-substrings\">Useful String Methods for Substrings<\/h2>\n\n\n\n<p>Besides slicing, Python offers methods to check for or find substrings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-in-operator-check-if-a-substring-exists\">1. <code>in<\/code> Operator: Check if a Substring Exists<\/h3>\n\n\n\n<p>You can use the <code>in<\/code> operator to check if a string contains a specific substring. It returns <code>True<\/code> or <code>False<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmain_string = &quot;hello python&quot;\n\n# Check if &quot;py&quot; is in the string\nhas_py = &quot;py&quot; in main_string\nprint(f&quot;&#039;py&#039; in string: {has_py}&quot;) \n\n# Check if &quot;java&quot; is in the string\nhas_java = &quot;java&quot; in main_string\nprint(f&quot;&#039;java&#039; in string: {has_java}&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#039;py&#039; in string: True\n&#039;java&#039; in string: False\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"2-find-method-get-the-starting-index\">2. <code>find()<\/code> Method: Get the Starting Index<\/h3>\n\n\n\n<p>The <code>find()<\/code> method returns the lowest index where the substring is found. If the substring is not found, it returns <code>-1<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmain_string = &quot;apple banana apple&quot;\n\n# Find the first occurrence of &quot;apple&quot;\nindex1 = main_string.find(&quot;apple&quot;)\nprint(f&quot;Index of &#039;apple&#039;: {index1}&quot;) \n\n# Find &quot;banana&quot;\nindex2 = main_string.find(&quot;banana&quot;)\nprint(f&quot;Index of &#039;banana&#039;: {index2}&quot;) \n\n# Try to find a non-existent substring\nindex3 = main_string.find(&quot;orange&quot;)\nprint(f&quot;Index of &#039;orange&#039;: {index3}&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIndex of &#039;apple&#039;: 0\nIndex of &#039;banana&#039;: 6\nIndex of &#039;orange&#039;: -1\n<\/pre><\/div>\n\n\n<p>You can also specify a start and end index for <code>find()<\/code> to search within a specific part of the string:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmain_string = &quot;apple banana apple&quot;\n\n# Find &quot;apple&quot; starting from index 1\nindex_after_first = main_string.find(&quot;apple&quot;, 1)\nprint(f&quot;Index of &#039;apple&#039; after index 0: {index_after_first}&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nIndex of &#039;apple&#039; after index 0: 13\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"3-rfind-method-get-the-last-starting-index\">3. <code>rfind()<\/code> Method: Get the Last Starting Index<\/h3>\n\n\n\n<p>The <code>rfind()<\/code> method is like <code>find()<\/code>, but it returns the highest (rightmost) index where the substring is found.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmain_string = &quot;apple banana apple&quot;\n\n# Find the last occurrence of &quot;apple&quot;\nlast_index = main_string.rfind(&quot;apple&quot;)\nprint(f&quot;Last index of &#039;apple&#039;: {last_index}&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nLast index of &#039;apple&#039;: 13\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"4-count-method-count-occurrences\">4. <code>count()<\/code> Method: Count Occurrences<\/h3>\n\n\n\n<p>The <code>count()<\/code> method returns the number of non-overlapping occurrences of a substring in the string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmain_string = &quot;banana split banana&quot;\n\n# Count occurrences of &quot;banana&quot;\nnum_bananas = main_string.count(&quot;banana&quot;)\nprint(f&quot;Number of &#039;banana&#039;: {num_bananas}&quot;) \n\n# Count occurrences of &quot;a&quot;\nnum_a = main_string.count(&quot;a&quot;)\nprint(f&quot;Number of &#039;a&#039;: {num_a}&quot;) \n<\/pre><\/div>\n\n\n<p>This code outputs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nNumber of &#039;banana&#039;: 2\nNumber of &#039;a&#039;: 7\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"best-practices-for-substrings\">Best Practices for Substrings<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Understand Immutability:<\/strong> Python strings are immutable. When you get a substring, you are not changing the original string. You are creating a new string.<\/li>\n\n\n\n<li><strong>Use <code>start:end<\/code> for Clarity:<\/strong> For simple extractions, <code>string[start:end]<\/code> is clear and easy to read.<\/li>\n\n\n\n<li><strong>Negative Indexing for End Parts:<\/strong> Use negative indices when you need parts from the end of a string.<\/li>\n\n\n\n<li><strong>Avoid Out-of-Bounds Errors:<\/strong> Slicing does not raise an error if your start or end index is out of bounds. It just returns an empty string or adjusts to the string boundaries. This is safe. For example, <code>my_string[100:200]<\/code> on a 10-character string returns <code>''<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>While slicing is safe and won't raise errors if your indices are out of bounds, learning the 'Pythonic' way to handle data ensures your code is efficient and easy for others to read. This is especially important as you transition from basic data entry to advanced automation. For a deeper dive into these best practices and to see how they apply to real-world data scenarios, signing up for a <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/python-fundamentals-for-beginners\" target=\"_blank\" rel=\"noreferrer noopener\">Free Python Course<\/a> is a great way to move beyond the basics.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Python substring is a part of a larger string. For example, \"py\" is a substring of \"python\". Knowing how to get and work with substrings is very useful for handling text data. It helps you extract specific parts of text. You can use it to get names from a full address, or extract specific [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":72956,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36796],"content_type":[],"class_list":["post-72935","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What is a Python Substring and How to Create One<\/title>\n<meta name=\"description\" content=\"A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.\" \/>\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.mygreatlearning.com\/blog\/python-substring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is a Python Substring?\" \/>\n<meta property=\"og:description\" content=\"A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-20T11:21:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-11T22:46:43+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1365\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"What is a Python Substring?\",\"datePublished\":\"2022-06-20T11:21:11+00:00\",\"dateModified\":\"2025-06-11T22:46:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/\"},\"wordCount\":771,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/iStock-1346047861.jpg\",\"keywords\":[\"python\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/\",\"name\":\"What is a Python Substring and How to Create One\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/iStock-1346047861.jpg\",\"datePublished\":\"2022-06-20T11:21:11+00:00\",\"dateModified\":\"2025-06-11T22:46:43+00:00\",\"description\":\"A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/iStock-1346047861.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/iStock-1346047861.jpg\",\"width\":1365,\"height\":768,\"caption\":\"python pip\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/python-substring\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"What is a Python Substring?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is a Python Substring and How to Create One","description":"A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.","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.mygreatlearning.com\/blog\/python-substring\/","og_locale":"en_US","og_type":"article","og_title":"What is a Python Substring?","og_description":"A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2022-06-20T11:21:11+00:00","article_modified_time":"2025-06-11T22:46:43+00:00","og_image":[{"width":1365,"height":768,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"What is a Python Substring?","datePublished":"2022-06-20T11:21:11+00:00","dateModified":"2025-06-11T22:46:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/"},"wordCount":771,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg","keywords":["python"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/","url":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/","name":"What is a Python Substring and How to Create One","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg","datePublished":"2022-06-20T11:21:11+00:00","dateModified":"2025-06-11T22:46:43+00:00","description":"A Python substring is a part of a larger string. Learn how to extract substrings and explore useful string methods for working with them.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/python-substring\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg","width":1365,"height":768,"caption":"python pip"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/python-substring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"What is a Python Substring?"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg",1365,768,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-768x432.jpg",768,432,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg",1365,768,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861.jpg",1365,768,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-640x768.jpg",640,768,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-96x96.jpg",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/iStock-1346047861-150x84.jpg",150,84,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"A Python substring is a part of a larger string. For example, \"py\" is a substring of \"python\". Knowing how to get and work with substrings is very useful for handling text data. It helps you extract specific parts of text. You can use it to get names from a full address, or extract specific&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/72935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=72935"}],"version-history":[{"count":35,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/72935\/revisions"}],"predecessor-version":[{"id":116947,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/72935\/revisions\/116947"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/72956"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=72935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=72935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=72935"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=72935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}