{"id":3569,"date":"2022-12-01T19:58:16","date_gmt":"2022-12-01T19:58:16","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3569"},"modified":"2022-12-01T19:58:18","modified_gmt":"2022-12-01T19:58:18","slug":"python-string-slicing","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-slicing\/","title":{"rendered":"Python String Slicing With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String Slicing<\/p>\n<p>String slicing simply means that we are getting a range of characters with the specified indexes. Here, we are going to provide the starting index and the ending index (note that the ending index is not included). This is like we are saying that from the given string, we want another string, which has the characters from the starting index to the ending index.<\/p>\n<p>For example, Let\u2019s consider the string &#8216;GyaniPandit&#8217;, and Let\u2019s say that we want another string from the given string, which consists of the characters from index 1 to index 5(actually index 5 is not included, so we are going to get the characters till the index 4). So, the new string would be &#8216;Yani&#8217;.<\/p>\n<p>So, first of all, Let\u2019s have a look at a program, which demonstrates the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3570 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING.jpg\" alt=\"\" width=\"644\" height=\"87\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING.jpg 644w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-300x41.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String Slicing<\/h2>\n<p>Notice how are we doing the string slicing. We are just using the same regular square brackets, which are used to access the individual characters in the string. In there, you can see two input numbers, which are the starting index, and the ending index respectively, separated by the colon. Here, the ending index is not included. So, the last character in another string would be the 4<sup>th<\/sup> index character, and the starting character would be the index 1 character. If you run the program, the output would be simply &#8216;Yani&#8217;. Here, we are assigning the new sliced string to another reference variable.<\/p>\n<p>One very important thing to note is that the original string is not going to be affected due to this slicing. Instead, a new string would be returned, which would be sliced, and will have the required characters.<\/p>\n<p>So, from the above example, you can simply try to print the original string, and it would print as it is.<\/p>\n<p>Now, Let\u2019s go beyond this, and try some other things related to the slicing of strings.<\/p>\n<p>You might be familiar now, that we are giving two inputs while slicing, and they are separated by a colon. The first value is the starting index, and another value is the ending index. But if some index is missing, like if the starting index or the ending index is missing? In that case, what output are we going to get? Are we going to get an error because the indexes are not given? Well, Let\u2019s explore it now.<\/p>\n<p>Let\u2019s consider that the starting index is missing. So, there is going to be no input value before the colon, and we are going to have the ending index after the colon. Have a look at the program, which demonstrates the same thing &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3571 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-1.jpg\" alt=\"\" width=\"637\" height=\"98\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-1.jpg 637w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-1-300x46.jpg 300w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/p>\n<p>As you can see, while we are slicing above, the starting index is missing. So, in the above program, are we going to get an error? Well, the answer is simply NO. We are not going to get an error, instead, if we are not giving the starting index, it will consider the 0 as the starting index. So, we are going to get the new string with the characters from the start to the 5<sup>th<\/sup> index (the 5<sup>th<\/sup> index is not included, so the last character would be the 4<sup>th<\/sup> index character).<\/p>\n<p>So, if we try to run the above program, the output would be &#8216;Gyani&#8217;.<\/p>\n<p>Similar to this, if the ending index is missing, again we are not going to get into errors, but instead, the string would be sliced till the end of the string. Have a look at the program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3572 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-2.jpg\" alt=\"\" width=\"634\" height=\"82\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-2.jpg 634w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-2-300x39.jpg 300w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/p>\n<p>This time, the new string that is going to be sliced, is going to have the characters from index 5 to the last of the string. So, if we run the program, the output is going to be &#8216;Pandit&#8217;. This is very easy to understand.<\/p>\n<p>Now, Let\u2019s consider that both indexes are missing now. In such a case, what are we going to get? Well, again if the indexes are missing, the defaults will be considered, which means the string is going to be sliced from the start to the end of the string. So, we are going to get a new string, which has all the characters.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3573 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-3.jpg\" alt=\"\" width=\"644\" height=\"108\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-3.jpg 644w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-3-300x50.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/p>\n<p>Now, in the above program, we can say that both indexes are missing. This time, we are going to get the new string, which is going to be sliced from the start to the end of the string. So, if we try to run the above program, we are going to get the whole string as the output. You can try running the above program and confirm the output.<\/p>\n<p>So, we can make use of string slicing, whenever we need to get another string, from our given string, which contains a certain range of characters from the given string. We are just required to use the square brackets, and we have to provide the starting index, and ending indexes separated by the colon, and if we are not specifying the values, the defaults would be considered.<\/p>\n<p>So,<\/p>\n<p>print(str1[:]) \u2192 prints whole string str1 as nothing was specified.<br \/>\nprint(str1[5:]) \u2192 prints from the 5<sup>th<\/sup> index to the end.<br \/>\nprint(str1[:5]) \u2192 prints from the start to the 5<sup>th<\/sup> index (but excluding the 5<sup>th<\/sup> index).<\/p>\n<p>Also, if we are trying to give some ending index, which is not even in the range of the string, are we going to get into errors? The simple answer is NO. In the above program, you can try giving the ending index as something which is greater than the length of the string, like 29. So, this time as well, we are not going to get into errors, but we would get the string up to the end of the string. So, Let\u2019s have a look at another program, which tries to demonstrate the same &#8211;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3574 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-4.jpg\" alt=\"\" width=\"649\" height=\"103\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-4.jpg 649w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-4-300x48.jpg 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>As you can see, we have given the starting index, as zero, and the ending index is actually something out of the range of the string, but still, we are not going to get any error, but we are going to get the string to end.<\/p>\n<p>But if you give starting index which is greater than the length of the string, then it would print nothing because it has nothing to print.<\/p>\n<p>Now, Let\u2019s say that you want to access the last element of the string, so for this, you would need to have the length of the string, since the last element index would be the length of the string minus 1, but what if you do not want to calculate the length, but still get the last character from the string? Well, this can be done easily. We just need to use something called negative indexing. This simply means that when we are giving the index as -1, then this would be the last character in the string, just like if we give 0 as an index, it is the first character in the string. So, going through other numbers like -2, -3, and so on, would get you 2<sup>nd<\/sup> and third characters from the last, and so on. In short, when we are using negative indexing, the characters are accessed from the last of the string. Have a look at the example, so that we can get more clarity &#8211;<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3575 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-5.jpg\" alt=\"\" width=\"644\" height=\"102\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-5.jpg 644w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/STRING-SLICING-5-300x48.jpg 300w\" sizes=\"auto, (max-width: 644px) 100vw, 644px\" \/><\/p>\n<p>As you can see in the above program, we are simply having a string, and then we are trying to access the characters using negative indexing. The index -1, means the last character. If you have a look at the output, it is simply going to print the last character, the last second, and the last third characters.<\/p>\n<p>So, you can use negative indexing as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String Slicing String slicing simply means that we are getting a range of characters with the specified indexes. Here, we are going to provide the starting index and the ending index (note that the ending index is not included). This is like we are saying that from the given string, we want another string, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3569","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3569"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3569\/revisions"}],"predecessor-version":[{"id":4161,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3569\/revisions\/4161"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}