{"id":733,"date":"2010-10-27T12:36:43","date_gmt":"2010-10-27T12:36:43","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/"},"modified":"2010-10-27T12:36:43","modified_gmt":"2010-10-27T12:36:43","slug":"7a-strings","status":"publish","type":"page","link":"https:\/\/olescs.hkmu.edu.hk\/python\/7a-strings\/","title":{"rendered":"7A: Strings"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p><em>Lesson 7 has three parts A, B, C which can be completed in any order.<\/em><\/p>\n<p>So far, we have been using strings (items of <code>str<\/code> type) only in simple ways. In this lesson we show how to manipulate strings: how to take them apart, combine them, and how to view the individual <em>characters<\/em> that make up a string.<\/p>\n<h2>What is a string?<\/h2>\n<p>All data stored on a computer is ultimately stored as a sequence of 0s and 1s. This includes text, digital books, images, songs, videos, and \"executable files\" like games and applications. Strings, an example of text data, are stored in the following way:<\/p>\n<ul>\n<li>a string is a sequence of characters (e.g., the string \"Hello, World!\" contains 13 characters including letters like \"H\", \"e\" and punctuation like \" \", \"!\"<\/li>\n<li>each character is actually represented by a number (e.g., \"H\" is represented by the number 40; this is its <a href=\"http:\/\/en.wikipedia.org\/wiki\/ASCII\">ASCII<\/a>\/<a href=\"http:\/\/en.wikipedia.org\/wiki\/Unicode\">Unicode<\/a> value)<\/li>\n<\/ul>\n<p>(Numbers are stored internally in a 0-1 <a href=\"http:\/\/en.wikipedia.org\/wiki\/Binary_numeral_system\">binary<\/a> format.)<\/p>\n<h2>Manipulating strings as sequences of characters: <code>S[]<\/code><\/h2>\n<p>In order to manipulate a string, we need to be able to access the individual characters that make up a string. In Python this is done in the following way: for a string <code>S<\/code> and an integer <code>index<\/code>, the notation<\/p>\n<pre>S[index]<\/pre>returns the character of <code>S<\/code> at position <code>index<\/code>. <strong>By convention<\/strong> the string starts at index 0: so <code>S[0]<\/code> is the first character, <code>S[1]<\/code> is the second character, etc. In \"Hello, World!\" the list of characters is:<\/p>\n<pre>Index: 0 \u00a01 \u00a02 \u00a03 \u00a04 \u00a05 \u00a06 \u00a07 \u00a08 \u00a09 10 11 12\nChar.: H \u00a0e \u00a0l \u00a0l \u00a0o \u00a0, \u00a0 \u00a0 W \u00a0o \u00a0r \u00a0l \u00a0d \u00a0!<\/pre>Note that the character at index 6 is a space.<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> In many other programming languages, there is a separate type for characters. In Python, characters are the same as length-1 strings, so <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/console\/?consolecode=ch%20%3D%20%22Hi%22%5B0%5D%0Aprint%28ch%29%0Aprint%28type%28ch%29%29%0Aprint%28len%28ch%29%29\" target=\"_blank\">their type is <code>str<\/code><\/a>. <\/span><\/td><\/table><\/p>\n<h2>Finding the number of characters in a string: <code>len<\/code><\/h2>\n<p>To get the number of characters in a string, we use the Python function <code>len<\/code>. For example, <code>len(\"Hello, World!\")<\/code> is 13.<\/p>\n<div class='pybox modeNeutral' id='pybox0'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Multiple Choice Exercise: <\/span><span class='title'>Last Character<\/span><\/div><div>What expression can be used to determine the <strong>last<\/strong> character in a string <code>S<\/code>?<\/div><label>Your choice: <\/label><select id=\"pyselect0\"><option value=\"d\" selected>Select one<\/option><option value=\"w\">S[len(S)+1]<\/option><option value=\"w\">S[len(S)]<\/option><option value=\"r\">S[len(S)-1]<\/option><\/select><div class='pyboxbuttons'><input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" name=\"slug\" value=\"7a.last\"\/><input type='submit' style='margin:5px;' value='Check answer' onClick='pbMultiCheck(0)'\/><\/div><div class=\"pbresults\" id=\"pyMultiResults0\"><\/div><div class=\"epilogue\">Correct! Although <code>len(S)<\/code> gives you the total number of characters in the string, since it starts with index <code>0<\/code>, the last character is at index <code>len(S)-1<\/code>.<\/div><\/div>\n<p>Here is an example of using <code>len<\/code> and <code>[]<\/code>, the two tools we just introduced.<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>String length and characters<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nmagic = 'Abracadabra!'\nprint(magic)\nprint(len(magic))\nprint(magic[0], magic[1], magic[len(magic)-1])<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(1)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(1,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"a42dfcea7bc7e33ec723fa318accf4fa\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<h2>Cutting strings: <code>S[:]<\/code><\/h2>\n<p>Cutting out some part of a string gives you a <strong>substring<\/strong>. For example, the strings \"eat\" and \"ted\" are substrings of \"repeated\". To extract a substring in Python, we use the syntax<\/p>\n<pre>S[firstIndex:tailIndex]<\/pre>to get the substring starting at index <strong>firstIndex<\/strong> and ending at <strong>tailIndex-1<\/strong>. Try to figure out the output of the following code before you run it.<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>Substrings<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nmyString = 'Weighty'\nprint(myString[1:6])\nprint('magic'[3:3])\nprint('chump'[0:4])<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"45119d9534f7774b00f02ed90785927c\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Note that in taking substrings, firstIndex <strong>is<\/strong> included, while the tailIndex is <strong>not<\/strong> included. This is a common source of errors. However, it has some nice effects. For example, because of this choice, the length of the substring\u00a0<code>S[i:j]<\/code>\u00a0is always\u00a0<code>j-i<\/code>. This convention is often depicted like a ruler: <img decoding=\"async\" style=\"border:none;text-align:center;margin-left:auto;margin-right:auto;float:none\" class=\" aligncenter\" src=\"..\/wp-content\/lesson_files\/img\/7\/substring.png\" alt=\"\" \/> <\/span><\/td><\/table><\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox3'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>String Shaving<\/span><\/div>Write a program which reads a string using <code>input()<\/code>, and outputs the same string but with the first and last character deleted. (You may assume the input string has length at least 2.) For example, on input <code>Fairy<\/code> a correct program will print <code>air<\/code>.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput3\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch3\" value=\"Input Switch\" onclick=\"pbInputSwitch(3,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect3' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(3,'7a.firstlast')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(3);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"6a18a1da5b23cc9c309009a50574b770\"\/>\n<div id='pbresults3' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(3);});pbInputSwitch(3,\"N\");<\/script>\n<\/p>\n<h2>Pasting strings: <code>+<\/code><\/h2>\n<p>We all know that 1+2=3. With strings, instead we get the following result:<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform4\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox4'>\n<div class=\"heading\"><span class='type'>Example: <\/span><span class='title'>String Addition<\/span><\/div> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode4' id='usercode4'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nresult = 'one' + 'two'\nprint(result)\nprint(len(result))<\/textarea><\/div>\n<div id='pbhistory4' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit4' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(4)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(4,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse4\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"4\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"17788658f853df2c4a295747b177d580\"\/>\n<div id='pbresults4' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit4\").value = \"Run program\";document.getElementById(\"inputInUse4\").value = \"N\";<\/script>\n<\/p>\n<p>As you can see, the effect of <code>S+T<\/code> is to create a new string that starts with <code>S<\/code> and has <code>T<\/code> immediately afterwards. This string-gluing operation is also called <strong>concatenation<\/strong>.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox5'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Heads and Tails<\/span><\/div>Write a program which reads a string using <code>input()<\/code>, and outputs the same string but with the first and last character <strong>exchanged<\/strong>. (You may assume the input string has length at least 2.) For example, on input <code>Fairy<\/code> a correct program will print <code>yairF<\/code>. Hint: use your solution to the previous program as part of the answer.<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput5\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch5\" value=\"Input Switch\" onclick=\"pbInputSwitch(5,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(5)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(5,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect5' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(5,'7a.swap')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(5);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"c5ee1959277f59799202dcf5b54357c5\"\/>\n<div id='pbresults5' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(5);});pbInputSwitch(5,\"N\");<\/script>\n<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> If you want to concatenate numbers, you need to convert them to str first. Otherwise you will get one of two errors, depending on the order you tried. Run this program to see the errors that can occur.<form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral scramble facultative' id='pybox6'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Rearrange the lines to see 2 concatenation errors<ul class=\"pyscramble\" name=\"pyscramble\" id=\"pyscramble6\">\n <li class=\"pyscramble\">print(&quot;high &quot; + 5)<\/li>\n <li class=\"pyscramble\">print(110 + &quot; percent&quot;)<\/li>\n<\/ul>\n<input type='hidden' id='usercode6' name='usercode6'\/>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"42f55e078525cdaf706e417b10880dee\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\nHere is a correct example: the\u00a0<code>str()<\/code>\u00a0function converts the number to a string before concatenation.<form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox7'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Converting a number to a string with <code>str()<\/code><div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 32px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=1 readonly='readonly'  style = 'height : 32px;'  class='pyboxCode RO'>\nprint(\"high \" + str(5))<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"daa4e48441b54e974cbdf799ca6e292e\"\/>\n<div id='pbresults7' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit7\").value = \"Run program\";document.getElementById(\"inputInUse7\").value = \"N\";<\/script>\n <\/span><\/td><\/table><\/p>\n<p>As we mentioned in Lesson 4, you can multiply strings and integers: <code>S * n<\/code> is short for <code>S + S + ... + S<\/code>.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox8'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>String multiplication<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode8' id='usercode8'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nprint(\"hots\" * 2)\nprint(\"mi\"+(\"s\"*2+\"i\")*2+\"ppi\")<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(8)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(8,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"925f6d8d9c2560c5cb1806844f936b21\"\/>\n<div id='pbresults8' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit8\").value = \"Run program\";document.getElementById(\"inputInUse8\").value = \"N\";<\/script>\n<\/p>\n<h2>Character codes: <code>ord<\/code>, <code>chr<\/code><a name=\"ord\"><\/a><\/h2>\n<p>As we mentioned in the introduction of this lesson, your computer actually represents every character as a number. Which number corresponds to which character? Generally, it can depend on which <em>encoding<\/em> your computer uses, but nearly all modern computers have a standard set of characters for the numbers between 32 and 255. Here is a list of the characters with numbers between 32 and 127:<\/p>\n<pre>ord: 32 \u00a033 \u00a034 \u00a035 \u00a036 \u00a037 \u00a038 \u00a039 \u00a040 \u00a041 \u00a042 \u00a043 \u00a044 \u00a045 \u00a046 \u00a047\nchr: \u00a0 \u00a0 \u00a0! \u00a0 \" \u00a0 # \u00a0 $ \u00a0 % \u00a0 &amp; \u00a0 ' \u00a0 ( \u00a0 ) \u00a0 * \u00a0 + \u00a0 , \u00a0 - \u00a0 . \u00a0 \/\nord: 48 \u00a049 \u00a050 \u00a051 \u00a052 \u00a053 \u00a054 \u00a055 \u00a056 \u00a057 \u00a058 \u00a059 \u00a060 \u00a061 \u00a062 \u00a063\nchr: \u00a00 \u00a0 1 \u00a0 2 \u00a0 3 \u00a0 4 \u00a0 5 \u00a0 6 \u00a0 7 \u00a0 8 \u00a0 9 \u00a0 : \u00a0 ; \u00a0 &lt; \u00a0 = \u00a0 &gt; \u00a0 ?\nord: 64 \u00a065 \u00a066 \u00a067 \u00a068 \u00a069 \u00a070 \u00a071 \u00a072 \u00a073 \u00a074 \u00a075 \u00a076 \u00a077 \u00a078 \u00a079\nchr: \u00a0@ \u00a0 A \u00a0 B \u00a0 C \u00a0 D \u00a0 E \u00a0 F \u00a0 G \u00a0 H \u00a0 I \u00a0 J \u00a0 K \u00a0 L \u00a0 M \u00a0 N \u00a0 O\nord: 80 \u00a081 \u00a082 \u00a083 \u00a084 \u00a085 \u00a086 \u00a087 \u00a088 \u00a089 \u00a090 \u00a091 \u00a092 \u00a093 \u00a094 \u00a095\nchr: \u00a0P \u00a0 Q \u00a0 R \u00a0 S \u00a0 T \u00a0 U \u00a0 V \u00a0 W \u00a0 X \u00a0 Y \u00a0 Z \u00a0 [ \u00a0 \\ \u00a0 ] \u00a0 ^ \u00a0 _\nord: 96 \u00a097 \u00a098 \u00a099 \u00a0100 101 102 103 104 105 106 107 108 109 110 111\nchr: \u00a0` \u00a0 a \u00a0 b \u00a0 c \u00a0 d \u00a0 e \u00a0 f \u00a0 g \u00a0 h \u00a0 i \u00a0 j \u00a0 k \u00a0 l \u00a0 m \u00a0 n \u00a0 o\nord: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127\nchr: \u00a0p \u00a0 q \u00a0 r \u00a0 s \u00a0 t \u00a0 u \u00a0 v \u00a0 w \u00a0 x \u00a0 y \u00a0 z \u00a0 { \u00a0 | \u00a0 } \u00a0 ~<\/pre>Later, in lesson 8, you will write a program to generate this table.<br \/>\nIt is not so useful to personally memorize the entire table, but there are some useful facts to remember:<\/p>\n<ul>\n<li>the lowercase characters a, b, c, ..., z have consecutive character codes<\/li>\n<li>the uppercase characters A, B, C, ..., Z have consecutive character codes<\/li>\n<li>the digit characters 0, 1, 2, ..., 9 have consecutive character codes<\/li>\n<\/ul>\n<p>Character 32 is a space, while character 127 is one of several special \"control\" characters. Some useful control characters are 9, which is tab, and 10 and 13 which are used for <a href=\"http:\/\/en.wikipedia.org\/wiki\/Newline\">newlines<\/a>.<\/p>\n<p>In Python, you can convert a character into its corresponding numerical code using the <code>ord<\/code> function. The <code>chr<\/code> function does the reverse: it takes a number as input, and returns the character with that code.<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox9'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>Examples of <code>ord<\/code> and <code>chr<\/code> <div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nprint(ord('a'), ord('b'), ord('A'), ord('B'), ord(' '))\nprint(chr(35), chr(43), chr(100))<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(9)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(9,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"a351e1806345a8af896f5e992e2af359\"\/>\n<div id='pbresults9' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit9\").value = \"Run program\";document.getElementById(\"inputInUse9\").value = \"N\";<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform10\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox10'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Next Letter<\/span><\/div>Write a program that takes a character as input (a string of length 1), which you should assume is an upper-case character; the output should be the next character in the alphabet. If the input is 'Z', your output should be 'A'. (You will need to use an <code>if<\/code> statement. For another hint <a class=\"hintlink\"  id=\"hintlink11\">click here<\/a>.)<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode10' id='usercode10'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory10' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput10\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit10' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch10\" value=\"Input Switch\" onclick=\"pbInputSwitch(10,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(10)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(10,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect10' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(10,'7a.lowercase')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(10);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse10\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"10\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"4ff89248f98a3a15ee42cf4bf13ff0cd\"\/>\n<div id='pbresults10' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(10);});pbInputSwitch(10,\"N\");<\/script>\n<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> Some systems only support printable characters between 32 and 127; others have printable characters up to 255 or 65535; in Unicode there are hundreds of thousands of characters. You can read more about the history <a href=\"http:\/\/en.wikipedia.org\/wiki\/American_Standard_Code_for_Information_Interchange\">here<\/a> or <a href=\"http:\/\/www.joelonsoftware.com\/articles\/Unicode.html\">here<\/a>. <\/span><\/td><\/table><\/p>\n<p>Here are two more exercises to finish the lesson.<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform12\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox12'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Pig Latin<\/span><\/div><strong>Pig Latin<\/strong> is a nonsense language. To transform a word from English to Pig Latin, you move the first letter to the end and add \"ay\" after that. For example, <em>monkey<\/em> becomes <em>onkeymay<\/em> in Pig Latin, and <em>word<\/em> becomes <em>ordway<\/em>. Write a program that takes a single word as input and translates it to Pig Latin. (In reality, Pig Latin has rules that are more complex than this, but we ignore them for the purposes of this exercise.)<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode12' id='usercode12'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory12' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput12\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit12' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch12\" value=\"Input Switch\" onclick=\"pbInputSwitch(12,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(12)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(12,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect12' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(12,'7a.piglatin')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(12);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse12\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"12\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"eb161e5a63f66ac0d38ec8929c1985e0\"\/>\n<div id='pbresults12' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(12);});pbInputSwitch(12,\"N\");<\/script>\n<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform13\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox13'>\n<img title='You have not yet completed this problem.' src='https:\/\/olescs.hkmu.edu.hk\/python\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>The Name Game<\/span><\/div>The <strong>Name Game<\/strong> lets you make a song out of any person's name. Listen to the song to get an idea of how it works: <div style='text-align: center;'>\n<iframe width='560' height='315' src='https:\/\/www.youtube.com\/embed\/NeF7jqf0GU4?rel=0' frameborder='0' allowfullscreen>\n<\/iframe>\n<\/div> Your program should take a person's name as input, for example \"pearl,\" and print out the song like <\/p>\n<pre>pearl, pearl, bo-bearl<br\/>banana-fana fo-fearl<br\/>fee-fi-mo-mearl<br\/>pearl!<\/pre> Note that the entire name appears three times; in addition the name appears three more times with the first letter replaced by b, f, or m. (In reality, the song has rules that are more complex than this, but we ignore them for the purposes of this exercise.)<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode13' id='usercode13'  cols=10 rows=20   class='pyboxCode RW'>\n# delete this comment and enter your code here\n<\/textarea><\/div>\n<div id='pbhistory13' class='flexcontain' style='display:none;'><\/div>\n<div name=\"pyinput\" id=\"pyinput13\">You may enter input for the program in the box below.<div class=\"pyboxTextwrap resizy\" style=\"height: 102px;\" ><textarea wrap=\"off\" name=\"userinput\" class=\"pyboxInput\" cols=10 rows=4><\/textarea><\/div><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit13' value=' '\/><\/td>\n<td><input type='button' name='switch' id=\"switch13\" value=\"Input Switch\" onclick=\"pbInputSwitch(13,'N')\" ><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(13)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(13,'N')\" ><\/td>\n<\/tr><\/table><select id='pbSelect13' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='history' data-pbonclick=\"historyClick(13,'7a.namegame')\" >History<\/option>\n<option name='help' data-pbonclick=\"helpClick(13);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"en-US\"\/><input type=\"hidden\" id=\"inputInUse13\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"13\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"79db51379b935112cdd529a487fb0d4b\"\/>\n<div id='pbresults13' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(13);});pbInputSwitch(13,\"N\");<\/script>\n<\/p>\n<p><em>Continue on to the next lesson!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lesson 7 has three parts A, B, C which can be completed in any order. So far, we have been using strings (items of str type) only in simple ways. In this lesson we show how to manipulate strings: how &hellip; <a href=\"https:\/\/olescs.hkmu.edu.hk\/python\/7a-strings\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-733","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/733"}],"collection":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":0,"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/pages\/733\/revisions"}],"wp:attachment":[{"href":"https:\/\/olescs.hkmu.edu.hk\/python\/wp-json\/wp\/v2\/media?parent=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}