{"id":93,"date":"2021-03-11T17:12:35","date_gmt":"2021-03-11T17:12:35","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=93"},"modified":"2021-05-13T16:24:06","modified_gmt":"2021-05-13T15:24:06","slug":"string-operations","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/string-operations\/","title":{"rendered":"String Operations"},"content":{"rendered":"<h1>Concatenation<\/h1>\n<p><em>Concatentation<\/em> is joining strings together.<\/p>\n<p>The SQA will use ampersand (&amp;), but Python uses plus (+) signs.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nfullname = forename + ' ' + surname\r\n#\r\n<\/pre>\n<hr \/>\n<h1>String Length<\/h1>\n<p>The len() function will return the number of characters in a string.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\npin = input(&quot;Enter your PIN:&quot;)\r\npinLength = len(pin)\r\nprint(f&quot;That has {pinLength} characters&quot;)\r\n#\r\n<\/pre>\n<hr \/>\n<h1>Characters and ASCII (for Higher)<\/h1>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">#\r\n# ord - returns the number representing the unicode code of a specified \r\nnumber = ord(&quot;A&quot;)\r\n\r\n# chr - returns the character that represents the specified\r\nletter = chr(65) \r\n \r\n#<\/pre>\n<hr \/>\n<h1>String Slicing (Substrings)\u00a0(for Higher)<\/h1>\n<p>Each character in a string has an\u00a0<em>index<\/em> number, starting at zero.<\/p>\n<pre>index     0 1 2 3 4 5 6 7 8\nstring    A n d e r s o n !<\/pre>\n<h3>Picking any single character:<\/h3>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nfirstLetter = string&#x5B;0]\r\nsecondLetter = string&#x5B;1]\r\n#\r\nlastLetter = string&#x5B;-1]\r\n#<\/pre>\n<h3>Picking several characters:<\/h3>\n<p>Give the index of the start letter, and the index of ending letter plus one.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nletters2and3 = string&#x5B;2:4]\r\nletters4to7 = string&#x5B;4:8]\r\n#<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Concatenation Concatentation is joining strings together. The SQA will use ampersand (&amp;), but Python uses plus (+) signs. # fullname = forename + &#8216; &#8216; + surname # String Length The len() function will return the number of characters in a string. # pin = input(&quot;Enter your PIN:&quot;) pinLength = len(pin) print(f&quot;That has {pinLength} characters&quot;) # Characters and ASCII (for Higher) # # ord &#8211; returns the number representing the<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/reference\/string-operations\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":294,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-93","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":7,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/93\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/93\/revisions\/617"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/294"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}