{"id":44,"date":"2021-03-11T16:13:09","date_gmt":"2021-03-11T16:13:09","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/?page_id=44"},"modified":"2021-06-10T09:35:40","modified_gmt":"2021-06-10T08:35:40","slug":"printing","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/nat4\/printing\/","title":{"rendered":"Printing"},"content":{"rendered":"<h2>Printing Text and Blank Lines<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nprint('This is a heading')\r\nprint()\r\nprint('This is another heading')\r\n<\/pre>\n<h2>Printing Variables (for debugging)<\/h2>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nprint(dice)\r\n#\r\n<\/pre>\n<h2>Printing Text and Variables (requires Python 3.6)<\/h2>\n<p>There must be an &#8220;f&#8221; before the first apostrophe, otherwise what is inside the curly brackets will be printed exactly.<br \/>\nThis is called an &#8220;f-string&#8221; (formatted-string)<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nprint(f'The capital of {country} is {capital}.')\r\n#\r\n<\/pre>\n<h2>Printing in Columns\u00a0(requires Python 3.6)<\/h2>\n<p>Each variable can be given a width, extra spaces will be added if needed.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nprint(f'The capital of {country:15} is {capital:15}.')\r\n#\r\n<\/pre>\n<h2>Printing Decimals\u00a0(requires Python 3.6)<\/h2>\n<p>Floating-point numbers can be printed out to a specified number of decimal places.<\/p>\n<p>This is not the same as rounding the numbers, which is permanent.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#\r\nprint(f'Price = \u00a3{price:.2f}')\r\n#\r\n<\/pre>\n<h1>Printing in Colour<\/h1>\n<p>This not required for the course, but does can easily make your programs more user friendly.<\/p>\n<p>Notes:<\/p>\n<ul>\n<li>The autoreset=True in the init() command will reset colours and styles after every print or input. If this option is left out, then colours and styles\u00a0remain active until they are changed or reset (Fore.RESET and Style.RESET_ALL).<\/li>\n<li>Be consistent with your colours &#8211; have one colour for input commands, another colour for errors, another colour for messages, etc.<\/li>\n<li>Thonny must be at least version 3.3.10 &#8211; the typed colour\/style for answers to input commands is fixed by the theme that you are using.<\/li>\n<\/ul>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom colorama import init, Fore, Style\r\ninit(autoreset=True)\r\n\r\nname = input(f&quot;{Fore.BLACK}Enter your name:&quot;)\r\nprint(f&quot;{Fore.BLUE}Hello {Style.BRIGHT}{name}{Style.NORMAL}&quot;)\r\n\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-626 size-full\" src=\"https:\/\/blogs.glowscotland.org.uk\/sh\/public\/ahscomputingpython\/uploads\/sites\/3268\/2021\/06\/10091107\/colorama-colours.png\" alt=\"\" width=\"526\" height=\"272\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/sh\/public\/ahscomputingpython\/uploads\/sites\/3268\/2021\/06\/10091107\/colorama-colours.png 526w, https:\/\/glow-prod-sh.s3.eu-west-1.amazonaws.com\/sh\/public\/ahscomputingpython\/uploads\/sites\/3268\/2021\/06\/10091107\/colorama-colours-300x155.png?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&amp;X-Amz-Security-Token=IQoJb3JpZ2luX2VjEN7%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCWV1LXdlc3QtMSJGMEQCIEPLl6wiwhlYYJohm3RKQe1QIErWy9%2BXpsqc13Ub%2F9LBAiANuAkL2KSUgMVvrunPmhHnhw3fUORVGkPPsRXaOHh%2BBCrCBQin%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAUaDDI3MzU3MDE5NTQzMyIM449NhUrugrVpnblkKpYFDo0b%2B682xMfITR%2BYsZ2loZsrVIiF9hVoMvZft%2BSK%2FTNge7J%2BbowZNRtOYSs5cmOtBMlDvv4G6GVw9wpO7wmWBEJ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HVf1MO17Jnn1XI9PC2lqxY0Fu5lp2X99QpZYi&amp;X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=ASIAT7MQN47URRJ7K6DA%2F20260404%2Feu-west-1%2Fs3%2Faws4_request&amp;X-Amz-Date=20260404T215520Z&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Expires=900&amp;X-Amz-Signature=6fba804b53653d5f21971faf5f46cab682d457b397a4428a4e6a268af15013c8 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Printing Text and Blank Lines print(&#8216;This is a heading&#8217;) print() print(&#8216;This is another heading&#8217;) Printing Variables (for debugging) # print(dice) # Printing Text and Variables (requires Python 3.6) There must be an &#8220;f&#8221; before the first apostrophe, otherwise what is inside the curly brackets will be printed exactly. This is called an &#8220;f-string&#8221; (formatted-string) # print(f&#8217;The capital of {country} is {capital}.&#8217;) # Printing in Columns\u00a0(requires Python 3.6) Each variable can<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/nat4\/printing\/\">Read More<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":297,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-44","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/44","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=44"}],"version-history":[{"count":18,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/44\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/44\/revisions\/631"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/pages\/297"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/sh\/ahscomputingpython\/wp-json\/wp\/v2\/media?parent=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}