{"id":19,"date":"2022-08-23T07:32:47","date_gmt":"2022-08-23T07:32:47","guid":{"rendered":"https:\/\/python.freelinuxtutorials.com\/?p=19"},"modified":"2022-08-24T08:25:49","modified_gmt":"2022-08-24T08:25:49","slug":"python-strings-indexing-and-slicing","status":"publish","type":"post","link":"https:\/\/python.freelinuxtutorials.com\/index.php\/2022\/08\/23\/python-strings-indexing-and-slicing\/","title":{"rendered":"Python>>Strings: Indexing and Slicing"},"content":{"rendered":"<p>I will be showing more examples of indexing and slicing as a form of my practice to familiarize this. From the previous lesson\/posts, the syntax for slicing as follows:<\/p>\n<blockquote><p>string[start : end : step]<\/p><\/blockquote>\n<p>Example1: (Indexing and Slicing in Strings)<\/p>\n<table width=\"301\">\n<tbody>\n<tr>\n<td width=\"23\">W<\/td>\n<td width=\"23\">r<\/td>\n<td width=\"23\">i<\/td>\n<td width=\"23\">t<\/td>\n<td width=\"23\">e<\/td>\n<td width=\"23\">C<\/td>\n<td width=\"17\">o<\/td>\n<td width=\"17\">d<\/td>\n<td width=\"17\">e<\/td>\n<td width=\"17\">O<\/td>\n<td width=\"19\">n<\/td>\n<td width=\"19\">l<\/td>\n<td width=\"19\">i<\/td>\n<td width=\"19\">n<\/td>\n<td width=\"19\">e<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>4<\/td>\n<td>5<\/td>\n<td>6<\/td>\n<td>7<\/td>\n<td>8<\/td>\n<td>9<\/td>\n<td>10<\/td>\n<td>11<\/td>\n<td>12<\/td>\n<td>13<\/td>\n<td>14<\/td>\n<\/tr>\n<tr>\n<td>-15<\/td>\n<td>-14<\/td>\n<td>-13<\/td>\n<td>-12<\/td>\n<td>-11<\/td>\n<td>-10<\/td>\n<td>-9<\/td>\n<td>-8<\/td>\n<td>-7<\/td>\n<td>-6<\/td>\n<td>-5<\/td>\n<td>-4<\/td>\n<td>-3<\/td>\n<td>-2<\/td>\n<td>-1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>-print the word &#8220;Online&#8221;<\/p>\n<blockquote><p>&gt;&gt; str=&#8217;WriteCodeOnline&#8217;<br \/>\n&gt;&gt;&gt; print(str)<br \/>\nWriteCodeOnline<br \/>\n&gt;&gt;&gt; print(str[9:])<br \/>\nOnline<\/p><\/blockquote>\n<p>-print the word &#8220;Onli&#8221;<\/p>\n<blockquote><p>&gt;&gt; print(str[9:13])<br \/>\nOnli<br \/>\n&gt;&gt;&gt;<\/p><\/blockquote>\n<p>-print the word &#8220;teCo&#8221;<\/p>\n<blockquote><p>&gt;&gt;&gt; print(str[3:7])<br \/>\nteCo<\/p><\/blockquote>\n<p>-print the word &#8220;Online&#8221; using the negative indexing<\/p>\n<blockquote><p>print(str[-6:])<br \/>\nOnline<\/p><\/blockquote>\n<p>-print the word &#8220;Onli&#8221; using negative indexing<\/p>\n<blockquote><p>&gt;&gt;&gt; print(str[-6:-2])<br \/>\nOnli<\/p><\/blockquote>\n<p>-print the word &#8220;teCo&#8221; using negative indexing<\/p>\n<blockquote><p>&gt;&gt; print(str[-12:-8])<br \/>\nteCo<\/p><\/blockquote>\n<p>Example2: (Indexing and Slicing in Lists)<\/p>\n<table style=\"border-collapse: collapse; width: 144pt;\" border=\"0\" width=\"192\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr style=\"height: 12.0pt;\">\n<td class=\"xl66\" style=\"height: 12pt; width: 48pt; text-align: left;\" width=\"64\" height=\"16\">Write<\/td>\n<td class=\"xl66\" style=\"border-left: none; width: 48pt; text-align: left;\" width=\"64\">Code<\/td>\n<td class=\"xl66\" style=\"border-left: none; width: 48pt; text-align: left;\" width=\"64\">Online<\/td>\n<\/tr>\n<tr style=\"height: 12.0pt;\">\n<td class=\"xl66\" style=\"height: 12.0pt; border-top: none;\" height=\"16\">0<\/td>\n<td class=\"xl66\" style=\"border-top: none; border-left: none;\">1<\/td>\n<td class=\"xl66\" style=\"border-top: none; border-left: none;\">2<\/td>\n<\/tr>\n<tr style=\"height: 12.0pt;\">\n<td class=\"xl66\" style=\"height: 12pt; border-top: none; text-align: left;\" height=\"16\">-3<\/td>\n<td class=\"xl66\" style=\"border-top: none; border-left: none; text-align: left;\">-2<\/td>\n<td class=\"xl66\" style=\"border-top: none; border-left: none; text-align: left;\">-1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Indexing:<\/p>\n<blockquote><p>strlist = [&#8216;Write&#8217;, &#8216;Code&#8217;, &#8216;Online&#8217;]<br \/>\n&gt;&gt;&gt; print(strlist)<br \/>\n[&#8216;Write&#8217;, &#8216;Code&#8217;, &#8216;Online&#8217;]<br \/>\n&gt;&gt;&gt; print(strlist[0])<br \/>\nWrite<br \/>\n&gt;&gt;&gt; print(strlist[2])<br \/>\nOnline<br \/>\n&gt;&gt;&gt; print(strlist[-2])<br \/>\nCode<\/p><\/blockquote>\n<blockquote><p>&gt;&gt; print(strlist[-2:-1])<br \/>\n[&#8216;Code&#8217;]<\/p><\/blockquote>\n<p>Example3: Steps(Stride)<\/p>\n<p>The &#8220;step&#8221; parameter is optional and the default is 1. It can be either positive or negative.<\/p>\n<p>As mentioned about, just want to repeat again<\/p>\n<p>string[start : end : step]<\/p>\n<p>start-position end-position increment<\/p>\n<p>e.g.<br \/>\nWRITECODE<\/p>\n<p>Let say we want to specify the step of slicing by 2<\/p>\n<p>[start:stop:2]<\/p>\n<blockquote><p>&gt;&gt; str = &#8216;WRITECODE&#8217;<br \/>\n&gt;&gt;&gt; print(str)<br \/>\nWRITECODE<br \/>\n&gt;&gt;&gt; print(str[::])<br \/>\nWRITECODE<br \/>\n&gt;&gt;&gt; print(str[::1])<br \/>\nWRITECODE<br \/>\n<strong>&gt;&gt;&gt; print(str[::2])<\/strong><br \/>\n<strong>WIEOE<\/strong><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Let take this example:<\/p>\n<blockquote><p>&gt;&gt; print(str[1:8:3])<br \/>\nRED<\/p><\/blockquote>\n<p>str[1:8:3] = W\u00a0 R\u00a0 \u00a0I\u00a0 \u00a0T\u00a0 E\u00a0 C\u00a0 O\u00a0 D\u00a0 E<\/p>\n<p>0\u00a0 \u00a0<strong>1<\/strong>\u00a0 2\u00a0 \u00a03\u00a0 4\u00a0 5\u00a0 6\u00a0 7\u00a0 \u00a08<\/p>\n<p>-starts\u00a0 from R and ends on D, as (E will not include)<\/p>\n<p>-steps will start counting from R, take R, then count 0,1,2,3 then take that character, from E count again 0,1,2,3 and take that character<\/p>\n<p>More example<\/p>\n<p>-Display letter &#8220;t&#8221; in one line for word &#8220;Writecode&#8221;<\/p>\n<blockquote><p>&gt;&gt; &#8216;Writecode'[3:4]<br \/>\n&#8216;t&#8217;<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>More indexing and slicing on next post<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I will be showing more examples of indexing and slicing as a form of my practice to familiarize this. From the previous lesson\/posts, the syntax for slicing as follows: string[start : end : step] Example1: (Indexing and Slicing in Strings) W r i t e C o d e O n l i n e&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[3],"tags":[4,7,8,6],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-data-types","tag-data-type","tag-indexing","tag-slicing","tag-strings"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":3,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}