{"id":2122,"date":"2018-10-08T12:11:15","date_gmt":"2018-10-08T11:11:15","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/?p=2122"},"modified":"2018-10-08T12:21:28","modified_gmt":"2018-10-08T11:21:28","slug":"higher-substring-operations","status":"publish","type":"post","link":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/2018\/10\/08\/higher-substring-operations\/","title":{"rendered":"Higher &#8211; Substring Operations"},"content":{"rendered":"<p><em>pre-defined functions (with parameters):<\/em><\/p>\n<ul>\n<li><em>to create substrings<\/em><\/li>\n<\/ul>\n<p>In Python we can treat strings as arrays of characters. We do this by using<\/p>\n<p style=\"padding-left: 30px\"><strong>string[start:stop:step]<\/strong><\/p>\n<p>where<\/p>\n<ul>\n<li><strong>start<\/strong> is the postion of the first character to be displayed.<\/li>\n<li><strong>stop<\/strong> is the postion to stop displaying characters (not displayed).<\/li>\n<li><strong>step<\/strong> is the step value, just like a for loop.<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p>So there is no need to use a function to slice a string.<\/p>\n<p>We can dispay any character by using<\/p>\n<p style=\"padding-left: 30px\">string[<strong>position<\/strong>] &#8211; where <strong>position<\/strong> is the index in the string (remember index starts at zero)<\/p>\n<p>We can display the <strong>left<\/strong> most portion of a string by using<\/p>\n<p style=\"padding-left: 30px\">string[<strong>:stop<\/strong>] &#8211; this means display all the character to the <strong>stop<\/strong> postion. So\u00a0&#8220;0123456789&#8221;[<strong>:6<\/strong>] would display the first 6 characters &#8216;012345&#8217;<\/p>\n<p>We can display the <strong>right<\/strong> most portion of a string by using<\/p>\n<p style=\"padding-left: 30px\">string[<strong>-stop:<\/strong>] &#8211; this displays all the\u00a0characters from the end to the stop postion. So\u00a0&#8220;0123456789&#8221;[<strong>-6:<\/strong>] would display the last 6 characters &#8216;456789&#8217;<\/p>\n<p>We can display characters in the <strong>middle<\/strong> of a string by using<\/p>\n<p style=\"padding-left: 30px\">string[<strong>start:stop<\/strong>] &#8211; displays the characters from start to stop (not displayed) . So\u00a0&#8220;0123456789&#8221;[<strong>2:5<\/strong>] would display the third (remember positions start at 0) to fifth characters &#8216;234&#8217;<\/p>\n<p>We can simplify the process by creating functions to slice the strings. In the example below I have called them <a href=\"https:\/\/exceljet.net\/excel-functions\/excel-left-function\">left<\/a>, <a href=\"https:\/\/exceljet.net\/excel-functions\/excel-right-function\">right<\/a> and <a href=\"https:\/\/exceljet.net\/excel-functions\/excel-mid-function\">mid <\/a>which mirror how the Excel functions work.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\ndef left(string,X):\r\n   # This function returns the left most X characters of the given string\r\n   return string&#x5B;:X]\r\n\r\ndef right(string,X):\r\n   # This function returns the right most X characters of the given string\r\n   return string&#x5B;-X:]\r\n\r\ndef mid(string,position,size):\r\n   # This function returns a substring of given size from string, starting from the position given\r\n   return string&#x5B;position-1:position-1+size]\r\n\r\ndef main():\r\n   string=&quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;\r\n   print(left(string,6)) # print the first 6 characters of the string\r\n   print(right(string,10)) # print the last 6 characters of the string\r\n   print(mid(string,13,5)) # print the 5 characters from the 13 character\r\n\r\nmain()\r\n\r\n<\/pre>\n<p>Further information can be found at:-<\/p>\n<ul>\n<li>https:\/\/www.w3schools.com\/python\/python_strings.asp<\/li>\n<li>https:\/\/realpython.com\/python-strings\/<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>pre-defined functions (with parameters): to create substrings In Python we can treat strings as arrays of characters. We do this by using string[start:stop:step] where start is the postion of the first character to be displayed. stop is the postion to stop displaying characters (not displayed). step is the step value, just like a for loop.<\/p>\n","protected":false},"author":32,"featured_media":1386,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[432098,17932],"tags":[432102,432104,432103,9199,432101],"class_list":["post-2122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-higher","category-computational-constructs","tag-left","tag-mid","tag-right","tag-string","tag-substring"],"jetpack_featured_media_url":"https:\/\/blogs.glowscotland.org.uk\/nl\/public\/ColtnessHS-ComputingScience\/uploads\/sites\/12638\/2016\/09\/Slide4.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/comments?post=2122"}],"version-history":[{"count":6,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2122\/revisions"}],"predecessor-version":[{"id":2128,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/posts\/2122\/revisions\/2128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/media\/1386"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/media?parent=2122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/categories?post=2122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/nl\/ColtnessHS-ComputingScience\/wp-json\/wp\/v2\/tags?post=2122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}