{"id":487,"date":"2022-01-15T05:48:54","date_gmt":"2022-01-15T05:48:54","guid":{"rendered":"https:\/\/techransom.com\/?p=487"},"modified":"2022-01-15T06:48:07","modified_gmt":"2022-01-15T06:48:07","slug":"ways-to-slice-the-pythons-substring","status":"publish","type":"post","link":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/","title":{"rendered":"Ways to slice the Python\u2019s substring"},"content":{"rendered":"\n

There are many ways in Python to use a string as a substring. This is often referred to as “slicing”. But if you are puzzled about the ways to do it, we are here to help you. The syntax is as follows:<\/p>\n

\n

string [start: end: step]<\/span><\/p>\n<\/blockquote>\n

Start<\/span> location: Substring start index. The characters in this index are contained in the substring. If start<\/span> is not included, it is assumed to be 0.<\/p>\n

end<\/span>: The final index of the substring.<\/span> The characters in this index are not in the substring. If the end<\/span> is not included or the specified value exceeds the length of the string, it is assumed to be the length of the string by default.<\/p>\n

Step<\/span>: Each “step” character after the current character is to be inserted. The default is 1. If no step<\/span> is included, it is considered equal to 1.<\/p>\n

\u00a0Basic use <\/strong><\/p>\n

string [start: end]<\/span>: Get all characters from the beginning(Start<\/span>) to the end<\/span> 1<\/p>\n

string [: end]<\/span>: Get all the characters from the beginning(Start<\/span>) to the end<\/span> 1<\/p>\n

string [start:]<\/span>: Get all the characters from the beginning(Start<\/span>) of the string Get all characters to the end<\/p>\n

string [start: end: step]<\/span>: Get all characters from start<\/span> to finish(end<\/span>) without step<\/span> characters<\/p>\n

You will understand it in a better manner with the help of examples.<\/p>\n

Examples<\/strong><\/p>\n

    \n
  1. \n

    Get the first 5 characters of the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n

    string = “freeCodeCamp<\/span>”<\/p>\n

    Print<\/span> (character string [0: 5<\/span>])<\/p><\/blockquote>\n

    Output:<\/p>\n

    > freeC<\/p><\/blockquote>\n

    Note: print (string [: 5])<\/span> provides the same result as a print (string [0: 5])<\/span>.<\/p>\n

      \n
    1. \n

      Gets a 4 character long Python\u2019s substring starting with the 3rd character of the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n

      string = “freeCodeCamp<\/span>”<\/p>\n

      Print<\/span> (character string [2: 6<\/span>])<\/p><\/blockquote>\n

      Edition:<\/p>\n

      > eeCo<\/p><\/blockquote>\n

        \n
      1. \n

        Get the last character in the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n

        string = “freeCodeCamp<\/span>”<\/p>\n

        Print<\/span> (character string [-1<\/span>])<\/p><\/blockquote>\n

        Edition:<\/p>\n

        > p<\/p><\/blockquote>\n

        Note that the start or end index can be negative. A negative index means that the count starts at the end of the string, not at the beginning of the string (right to left).<\/p>\n

        Index 1 represents the last character in the string and 2 represents the penultimate character.<\/p>\n

          \n
        1. \n

          Get the last 5 characters of the string <\/strong><\/h2>\n<\/li>\n<\/ol>\n

          string = “freeCodeCamp<\/span>”<\/p>\n

          Print<\/span> (character string [5:<\/span>])<\/p><\/blockquote>\n

          Edition:<\/p>\n

          > eCamp<\/p><\/blockquote>\n

            \n
          1. \n

            Get a substring containing all but the last 4 characters and the first character <\/strong><\/h2>\n<\/li>\n<\/ol>\n

            string = “freeCodeCamp<\/span>”<\/p>\n

            Print<\/span> (character string [1: 4<\/span>])<\/p><\/blockquote>\n

            Output:<\/p>\n

            > reeCode<\/p><\/blockquote>\n

              \n
            1. \n

              Get every other character from a string <\/strong><\/h2>\n<\/li>\n<\/ol>\n

              string = “freeCodeCamp<\/span>”<\/p>\n

              Print<\/span> (character string [:: 2<\/span>])<\/p><\/blockquote>\n

              Output:<\/p>\n

              > feCdCm<\/p><\/blockquote>\n

              We hope that you can do so without any confusion and ease.<\/p>\n","protected":false},"excerpt":{"rendered":"

              There are many ways in Python to use a string as a substring. This is often referred to as “slicing”….<\/p>\n","protected":false},"author":1,"featured_media":496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-487","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pc"],"yoast_head":"\nWays to slice the Python\u2019s substring - Tech Ransom<\/title>\n<meta name=\"description\" content=\"Python\u2019s substring: There are many ways in Python to use a string as a substring. This is often referred to as "slicing". But if you are puzzled about the ways to do it, we are here to help you.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ways to slice the Python\u2019s substring - Tech Ransom\" \/>\n<meta property=\"og:description\" content=\"Python\u2019s substring: There are many ways in Python to use a string as a substring. This is often referred to as "slicing". But if you are puzzled about the ways to do it, we are here to help you.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\" \/>\n<meta property=\"og:site_name\" content=\"Tech Ransom\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-15T05:48:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-15T06:48:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1895\" \/>\n\t<meta property=\"og:image:height\" content=\"762\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"techransom\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"techransom\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\"},\"author\":{\"name\":\"techransom\",\"@id\":\"https:\/\/techransom.com\/#\/schema\/person\/f244ce633171a3d5db2c6136d176e4c3\"},\"headline\":\"Ways to slice the Python\u2019s substring\",\"datePublished\":\"2022-01-15T05:48:54+00:00\",\"dateModified\":\"2022-01-15T06:48:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\"},\"wordCount\":400,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/techransom.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png\",\"articleSection\":[\"PC\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\",\"url\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\",\"name\":\"Ways to slice the Python\u2019s substring - Tech Ransom\",\"isPartOf\":{\"@id\":\"https:\/\/techransom.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png\",\"datePublished\":\"2022-01-15T05:48:54+00:00\",\"dateModified\":\"2022-01-15T06:48:07+00:00\",\"description\":\"Python\u2019s substring: There are many ways in Python to use a string as a substring. This is often referred to as \\\"slicing\\\". But if you are puzzled about the ways to do it, we are here to help you.\",\"breadcrumb\":{\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage\",\"url\":\"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png\",\"contentUrl\":\"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png\",\"width\":1895,\"height\":762,\"caption\":\"Python substring\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/techransom.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ways to slice the Python\u2019s substring\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/techransom.com\/#website\",\"url\":\"https:\/\/techransom.com\/\",\"name\":\"Tech Ransom\",\"description\":\"All Tech Updates and News\",\"publisher\":{\"@id\":\"https:\/\/techransom.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/techransom.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/techransom.com\/#organization\",\"name\":\"Tech Ransom\",\"url\":\"https:\/\/techransom.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techransom.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/techransom.com\/wp-content\/uploads\/2021\/02\/TechRansom-1.png\",\"contentUrl\":\"https:\/\/techransom.com\/wp-content\/uploads\/2021\/02\/TechRansom-1.png\",\"width\":300,\"height\":140,\"caption\":\"Tech Ransom\"},\"image\":{\"@id\":\"https:\/\/techransom.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/techransom.com\/#\/schema\/person\/f244ce633171a3d5db2c6136d176e4c3\",\"name\":\"techransom\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techransom.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ebc185f55ac13108bb6b2ac1e149199b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ebc185f55ac13108bb6b2ac1e149199b?s=96&d=mm&r=g\",\"caption\":\"techransom\"},\"sameAs\":[\"https:\/\/techransom.com\"],\"url\":\"https:\/\/techransom.com\/author\/techransom\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ways to slice the Python\u2019s substring - Tech Ransom","description":"Python\u2019s substring: There are many ways in Python to use a string as a substring. This is often referred to as \"slicing\". But if you are puzzled about the ways to do it, we are here to help you.","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:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/","og_locale":"en_US","og_type":"article","og_title":"Ways to slice the Python\u2019s substring - Tech Ransom","og_description":"Python\u2019s substring: There are many ways in Python to use a string as a substring. This is often referred to as \"slicing\". But if you are puzzled about the ways to do it, we are here to help you.","og_url":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/","og_site_name":"Tech Ransom","article_published_time":"2022-01-15T05:48:54+00:00","article_modified_time":"2022-01-15T06:48:07+00:00","og_image":[{"width":1895,"height":762,"url":"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png","type":"image\/png"}],"author":"techransom","twitter_card":"summary_large_image","twitter_misc":{"Written by":"techransom","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#article","isPartOf":{"@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/"},"author":{"name":"techransom","@id":"https:\/\/techransom.com\/#\/schema\/person\/f244ce633171a3d5db2c6136d176e4c3"},"headline":"Ways to slice the Python\u2019s substring","datePublished":"2022-01-15T05:48:54+00:00","dateModified":"2022-01-15T06:48:07+00:00","mainEntityOfPage":{"@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/"},"wordCount":400,"commentCount":0,"publisher":{"@id":"https:\/\/techransom.com\/#organization"},"image":{"@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png","articleSection":["PC"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/","url":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/","name":"Ways to slice the Python\u2019s substring - Tech Ransom","isPartOf":{"@id":"https:\/\/techransom.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage"},"image":{"@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage"},"thumbnailUrl":"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png","datePublished":"2022-01-15T05:48:54+00:00","dateModified":"2022-01-15T06:48:07+00:00","description":"Python\u2019s substring: There are many ways in Python to use a string as a substring. This is often referred to as \"slicing\". But if you are puzzled about the ways to do it, we are here to help you.","breadcrumb":{"@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#primaryimage","url":"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png","contentUrl":"https:\/\/techransom.com\/wp-content\/uploads\/2022\/01\/Python-substring.png","width":1895,"height":762,"caption":"Python substring"},{"@type":"BreadcrumbList","@id":"https:\/\/techransom.com\/ways-to-slice-the-pythons-substring\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techransom.com\/"},{"@type":"ListItem","position":2,"name":"Ways to slice the Python\u2019s substring"}]},{"@type":"WebSite","@id":"https:\/\/techransom.com\/#website","url":"https:\/\/techransom.com\/","name":"Tech Ransom","description":"All Tech Updates and News","publisher":{"@id":"https:\/\/techransom.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techransom.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techransom.com\/#organization","name":"Tech Ransom","url":"https:\/\/techransom.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techransom.com\/#\/schema\/logo\/image\/","url":"https:\/\/techransom.com\/wp-content\/uploads\/2021\/02\/TechRansom-1.png","contentUrl":"https:\/\/techransom.com\/wp-content\/uploads\/2021\/02\/TechRansom-1.png","width":300,"height":140,"caption":"Tech Ransom"},"image":{"@id":"https:\/\/techransom.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/techransom.com\/#\/schema\/person\/f244ce633171a3d5db2c6136d176e4c3","name":"techransom","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techransom.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ebc185f55ac13108bb6b2ac1e149199b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ebc185f55ac13108bb6b2ac1e149199b?s=96&d=mm&r=g","caption":"techransom"},"sameAs":["https:\/\/techransom.com"],"url":"https:\/\/techransom.com\/author\/techransom\/"}]}},"_links":{"self":[{"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/posts\/487"}],"collection":[{"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/comments?post=487"}],"version-history":[{"count":8,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/posts\/487\/revisions"}],"predecessor-version":[{"id":495,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/posts\/487\/revisions\/495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/media\/496"}],"wp:attachment":[{"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/media?parent=487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/categories?post=487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techransom.com\/wp-json\/wp\/v2\/tags?post=487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}