{"id":11676,"date":"2015-08-05T07:52:37","date_gmt":"2015-08-05T07:52:37","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=11676"},"modified":"2015-08-05T07:55:59","modified_gmt":"2015-08-05T07:55:59","slug":"python-printing-joined-strings","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/python-printing-joined-strings\/","title":{"rendered":"Python:  printing joined strings"},"content":{"rendered":"<h1>The tutor brings up a couple of quick points about printing strings in Python.<\/h1>\n<p>In computer science, <em>concatenate<\/em> is another way of saying <em>join<\/em>.  In Python, it&#8217;s done with the &#8220;plus&#8221; sign:<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\ns0=&#8221;Hello.&#8221;<br \/>\ns1=&#8221;  How are you?&#8221;<br \/>\nprint(s0+s1)<br \/>\n<\/span><br \/>\ngives the output<br \/>\n<span style=\"font-family:monospace\"><br \/>\nHello.  How are you?<br \/>\n<\/span><\/p>\n<p>However, when printing a string joined to a number, the number needs to be converted to a string:<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\nval0=7*8<br \/>\nmessage=&#8221;Seven times eight equals &#8221;<br \/>\nprint(message+val0) <span style=\"color:orange\">#will produce an error<\/span><br \/>\n<\/span><\/p>\n<p>Changing <span style=\"font-family:monospace\">val0<\/span> to a string like so<\/p>\n<p><span style=\"font-family:monospace\">str(val0)<\/span><\/p>\n<p>will correct the problem:<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\nval0=7*8<br \/>\nmessage=&#8221;Seven times eight equals &#8221;<br \/>\nprint(message+str(val0)) <span style=\"color:orange\">#will work:)<\/span><br \/>\n<\/span><\/p>\n<p>produces the output<\/p>\n<p><span style=\"font-family:monospace\">Seven times eight equals 56<\/span><\/p>\n<p>HTH:)<\/p>\n<p>Source:<\/p>\n<p>Donaldson, Toby.  <u>Python<\/u>, 3rd edition.  Peachpit Press, 2014.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor brings up a couple of quick points about printing strings in Python. In computer science, concatenate is another way of saying join. In Python, it&#8217;s done with the &#8220;plus&#8221; sign: s0=&#8221;Hello.&#8221; s1=&#8221; How are you?&#8221; print(s0+s1) gives the &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/python-printing-joined-strings\/\"> <span class=\"screen-reader-text\">Python:  printing joined strings<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[930,970,969],"class_list":["post-11676","post","type-post","status-publish","format-standard","hentry","category-computer-science","tag-python","tag-python-str-operator","tag-python-string-concatenation"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=11676"}],"version-history":[{"count":15,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11676\/revisions"}],"predecessor-version":[{"id":11691,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11676\/revisions\/11691"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=11676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=11676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=11676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}