{"id":4403,"date":"2020-03-29T12:32:53","date_gmt":"2020-03-29T12:32:53","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4403"},"modified":"2022-08-06T13:28:27","modified_gmt":"2022-08-06T13:28:27","slug":"slice-strings-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/slice-strings-in-python","title":{"rendered":"How to Slice Strings in Python?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>In this tutorial, we are going to learn how we can slice strings in Python.<\/p>\n\n\n\n<p>Python supports the slicing of strings. It is the creation of a new sub-string from the given string on the basis of the user-defined starting and ending indices. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ways to Slice Strings in Python<\/h2>\n\n\n\n<p>If you want to slice strings in Python, it&#8217;s going to be as simple as this one line below. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nres_s = s&#x5B; start_pos:end_pos:step ]\n<\/pre><\/div>\n\n\n<p>Here, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>res_s<\/strong> stores the returned sub-string,<\/li><li>s is the given string,<\/li><li><strong>start_pos<\/strong> is the starting index from which we need to slice the string s,<\/li><li><strong>end_pos<\/strong> is the ending index, before which the slicing operation would end, <\/li><li><strong>step<\/strong> is the steps the slicing process would take from start_pos to end_pos.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: All of the above three parameters are optional. By default, <code>start_pos<\/code> is set to <strong>0<\/strong>, <code>end_pos<\/code> is considered equal to the length of the string, and <code>step<\/code> is set to <strong>1<\/strong>.<\/p>\n\n\n\n<p>Now let us take some examples to understand how to slice strings in Python in a better way. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Slice Strings in Python &#8211; Examples<\/h2>\n\n\n\n<p>Slicing Python strings can be done in different ways.<\/p>\n\n\n\n<p>Usually, we access string elements(characters) using simple indexing that starts from <em>0<\/em> up to <strong>n-1<\/strong>(n is the length of the string). Hence for accessing the <strong>1st<\/strong> element of a string <code>string1<\/code>, we can simply use the below code. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ns1 = String1&#x5B;0]\n<\/pre><\/div>\n\n\n<p>Again, there is another way to access these characters, that is, using <strong>negative indexing<\/strong>. Negative indexing starts from <strong>-1<\/strong> to <strong>-n<\/strong>(n is the length for the given string). Note, negative indexing is done from the other end of the string. Hence, to access the first character this time we need to follow the below-given code. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ns1 = String\ufeff1&#x5B;-n]\n<\/pre><\/div>\n\n\n<p>Now let us look at some ways following which we can slice a string using the above concept. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Slice Strings in Python with Start and End<\/h3>\n\n\n\n<p>We can easily slice a given string by mentioning the starting and ending indices for the desired sub-string we are looking for. Look at the example given below, it explains string slicing using starting and ending indices for both usual and negative indexing method. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#string slicing with two parameters\ns = &quot;Hello World!&quot;\n\nres1 = s&#x5B;2:8]\nres2 = s&#x5B;-4:-1] #using negative indexing\n\nprint(&quot;Result1 = &quot;,res1)\nprint(&quot;Result2 = &quot;,res2)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nResult1 =  llo Wo\nResult2 =  rld\n<\/pre><\/div>\n\n\n<p>Here,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We initialize a string, <code>s<\/code> as <strong>&#8220;Hello World!&#8221;<\/strong>, <\/li><li>At first, we slice the given string with starting index <strong>2<\/strong> and ending index as <strong>8<\/strong>. This means that the resultant sub-string would contain the characters from <strong>s[2]<\/strong> to <strong>s[8-1]<\/strong>, <\/li><li>Similarly, for the next one, the resultant sub-string should contain characters from <strong>s[-4]<\/strong> to <strong>s[(-1)-1]<\/strong>.<\/li><\/ul>\n\n\n\n<p>Hence, our output is justified.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Slice Strings Using Only the Start or End<\/h3>\n\n\n\n<p>As mentioned earlier, all of the three parameters for string slicing are optional. Hence,  we can easily accomplish our tasks using one parameter. Look at the code below to get a clear understanding. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#string slicing with one parameter\ns1= &quot;Charlie&quot;\ns2=&quot;Jordan&quot;\n\nres1 = s1&#x5B;2:] #default value of ending position is set to the length of string\nres2 = s2&#x5B;:4] #default value of starting position is set to 0\n\nprint(&quot;Result1 = &quot;,res1)\nprint(&quot;Result2 = &quot;,res2)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nResult1 =  arlie\nResult2 =  Jord\n<\/pre><\/div>\n\n\n<p>Here, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We firstly initialize two strings, <strong>s1<\/strong> and <strong>s2<\/strong>, <\/li><li>For slicing both of them we just mention the <strong>start_pos<\/strong> for s1, and <strong>end_pos<\/strong> for s2 only, <\/li><li>Hence for <strong>res1<\/strong>, it contains a sub-string of s1 from index 2(as mentioned) up to the last(by default it is set to n-1). Whereas for res2, the range of the indices lies from 0 upto 4(mentioned). <\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. Slice Strings in Python with Step Parameter<\/h3>\n\n\n\n<p>The <code>step<\/code> value decides the jump the slicing operation would take from one index to the other. Look at the example below carefully. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#string slicing with step parameter\ns= &quot;Python&quot;\ns1=&quot;Kotlin&quot;\n\nres = s&#x5B;0:5:2]\nres1 = s1&#x5B;-1:-4:-2] #using negative parameters\n\nprint(&quot;Resultant sliced string = &quot;,res)\nprint(&quot;Resultant sliced string(negative parameters) = &quot;,res1)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nResultant sliced string =  Pto\nResultant sliced string(negative parameters) =  nl\n<\/pre><\/div>\n\n\n<p>In the code above, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>We initialize two strings <strong>s<\/strong> and <strong>s1<\/strong>, and try to slice them for the given  starting and ending indices as we did for our first example, <\/li><li>But this time we have mentioned a <strong>step<\/strong> value which was set to 1 by default for the previous examples, <\/li><li>For res, having a step size 2 means, that while the traversal to get the substring from index 0 to 4, each time the index would be increased by a value of 2. That is the first character is <strong>s[0]<\/strong> (&#8216;P&#8217;), the next characters in the sub-string would be <strong>s[0+2]<\/strong>, and <strong>s[2+2]<\/strong>, until the index is just less than 5.<\/li><li>For the next one i.e. <strong>res1<\/strong>, the step mentioned is (-2). Hence, similar to the previous case, the characters in the substring would be <strong>s1[-1]<\/strong> , then <strong>s1[(-1)+(-2)]<\/strong> or <strong>s1[-3]<\/strong> until the index is just less than (-4).<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. Reversing a String using Slicing in Python<\/h3>\n\n\n\n<p>With the use of negative index string slicing in Python, we can also reverse the string and store it in another variable. For doing so we just need to mention a <code>step<\/code> size of <strong>(-1)<\/strong>. <\/p>\n\n\n\n<p>Let us see how that works in the example given below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#reversing string using string slicing\ns= &quot;AskPython&quot;\nrev_s = s&#x5B;::-1] #reverse string stored into rev_s\n\nprint(rev_s)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnohtyPksA\n<\/pre><\/div>\n\n\n<p>As we can see, the string s is reversed and stored into <code>rev_s<\/code>. <strong>Note<\/strong>: For this case too, the original string remains intact and untouched.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>So in this tutorial, we learned about the string slicing methodology and its different forms. Hope, the readers had a clear understanding of the topic.<\/p>\n\n\n\n<p>For any further questions related to this topic, feel free to use the comments below. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Slice Strings &#8211; Journal Dev Post,<\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/1010961\/ways-to-slice-a-string\" target=\"_blank\" rel=\"noopener\">Ways to slice a string?<\/a> &#8211; StackOverflow Question. <\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this tutorial, we are going to learn how we can slice strings in Python. Python supports the slicing of strings. It is the creation of a new sub-string from the given string on the basis of the user-defined starting and ending indices. Ways to Slice Strings in Python If you want to slice [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":4411,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-4403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4403","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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4403"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4403\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4411"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}