{"id":1147,"date":"2019-06-23T18:37:34","date_gmt":"2019-06-23T18:37:34","guid":{"rendered":"http:\/\/learnlearn.uk\/python\/?page_id=1147"},"modified":"2023-10-25T10:08:17","modified_gmt":"2023-10-25T10:08:17","slug":"python-console-colors","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/python\/python-console-colors\/","title":{"rendered":"Python Console Colors"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Video<\/h2>\n<div class=\"tabcontent\">\n\n<h3>How to add colored text and background to your Python Programs<\/h3>\n<p>In this video tutorial you will learn how to add colors to your Python programs. You can install 3rd party modules like colorama, but sometimes it&#8217;s better if you just write your own!<\/p>\n<h2>Video Tutorial<\/h2>\n<p><a href=\"https:\/\/drive.google.com\/open?id=1CigLuRvwwk0TPy2zE_XNk6MnEj3KSVY6\">Youtube blocked in your school? Here&#8217;s the Google Drive Link instead.<\/a><\/p>\n<div class=\"nv-iframe-embed\">\n<div class=\"container-lazyload preview-lazyload container-youtube js-lazyload--not-loaded\"><a href=\"https:\/\/youtu.be\/u4QmAIoo4i0\" class=\"lazy-load-youtube preview-lazyload preview-youtube\" data-video-title=\"Python Console Colors - Easy Tutorial\" title=\"Play video &quot;Python Console Colors - Easy Tutorial&quot;\">https:\/\/youtu.be\/u4QmAIoo4i0<\/a><noscript>Video can&#8217;t be loaded because JavaScript is disabled: <a href=\"https:\/\/youtu.be\/u4QmAIoo4i0\" title=\"Python Console Colors - Easy Tutorial\">Python Console Colors &#8211; Easy Tutorial (https:\/\/youtu.be\/u4QmAIoo4i0)<\/a><\/noscript><\/div>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Full Code<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Full Code<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">os<\/span>\r\n\r\nos.system(<span style=\"color: #aa5500;\">\"cls\"<\/span>) <span style=\"color: #aaaaaa; font-style: italic;\">#use this for windows. change to os.system(\"clear\") for linux<\/span>\r\n\r\nCOLORS = {\\\r\n<span style=\"color: #aa5500;\">\"black\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[30;1m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"red\"<\/span>: <span style=\"color: #aa5500;\">\"\\u001b[31;1m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"green\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[32m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"yellow\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[33;1m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"blue\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[34;1m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"magenta\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[35m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"cyan\"<\/span>: <span style=\"color: #aa5500;\">\"\\u001b[36m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"white\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[37m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"yellow-background\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[43m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"black-background\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[40m\"<\/span>,\r\n<span style=\"color: #aa5500;\">\"cyan-background\"<\/span>:<span style=\"color: #aa5500;\">\"\\u001b[46;1m\"<\/span>,\r\n}\r\n<span style=\"color: #aaaaaa; font-style: italic;\">#You can add more colors and backgrounds to the dictionary if you like.<\/span>\r\n\r\n\r\n<span style=\"color: #0000aa;\">def<\/span> <span style=\"color: #00aa00;\">colorText<\/span>(text):\r\n    <span style=\"color: #0000aa;\">for<\/span> color <span style=\"color: #0000aa;\">in<\/span> COLORS:\r\n        text = text.replace(<span style=\"color: #aa5500;\">\"[[\"<\/span> + color + <span style=\"color: #aa5500;\">\"]]\"<\/span>, COLORS[color])\r\n    <span style=\"color: #0000aa;\">return<\/span> text\r\n\r\n\r\n<span style=\"color: #aaaaaa; font-style: italic;\">#Example printing out some text<\/span>\r\nhello = <span style=\"color: #aa5500;\">\"[[red]]hello [[blue]]world[[white]]\"<\/span>\r\n<span style=\"color: #0000aa;\">print<\/span>(colorText(hello))\r\n\r\n<span style=\"color: #aaaaaa; font-style: italic;\">#Example printing out an ASCII file<\/span>\r\nf  = <span style=\"color: #00aaaa;\">open<\/span>(<span style=\"color: #aa5500;\">\"pythonlogo2.txt\"<\/span>,<span style=\"color: #aa5500;\">\"r\"<\/span>)\r\nascii = <span style=\"color: #aa5500;\">\"\"<\/span>.join(f.readlines())\r\n<span style=\"color: #0000aa;\">print<\/span>(colorText(ascii)) \r\n<\/pre>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>How to add colored text and background to your Python Programs In this video tutorial you will learn how to add colors to your Python programs. You can install 3rd party modules like colorama, but sometimes it&#8217;s better if you just write your own! Video Tutorial Youtube blocked in your school? Here&#8217;s the Google Drive&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/python\/python-console-colors\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Python Console Colors<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":70,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Console Colors - Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/python\/python-console-colors\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Console Colors - Python\" \/>\n<meta property=\"og:description\" content=\"How to add colored text and background to your Python Programs In this video tutorial you will learn how to add colors to your Python programs. You can install 3rd party modules like colorama, but sometimes it&#8217;s better if you just write your own! Video Tutorial Youtube blocked in your school? Here&#8217;s the Google Drive&hellip;&nbsp;Read More &raquo;Python Console Colors\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/python\/python-console-colors\/\" \/>\n<meta property=\"og:site_name\" content=\"Python\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-25T10:08:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/python\/python-console-colors\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/python-console-colors\/\",\"name\":\"Python Console Colors - Python\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\"},\"datePublished\":\"2019-06-23T18:37:34+00:00\",\"dateModified\":\"2023-10-25T10:08:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/python-console-colors\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/python\/python-console-colors\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/python\/python-console-colors\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Console Colors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"name\":\"Python\",\"description\":\"Programming\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/python\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\",\"name\":\"Python\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Python\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Console Colors - Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/python\/python-console-colors\/","og_locale":"en_GB","og_type":"article","og_title":"Python Console Colors - Python","og_description":"How to add colored text and background to your Python Programs In this video tutorial you will learn how to add colors to your Python programs. You can install 3rd party modules like colorama, but sometimes it&#8217;s better if you just write your own! Video Tutorial Youtube blocked in your school? Here&#8217;s the Google Drive&hellip;&nbsp;Read More &raquo;Python Console Colors","og_url":"https:\/\/learnlearn.uk\/python\/python-console-colors\/","og_site_name":"Python","article_modified_time":"2023-10-25T10:08:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/python\/python-console-colors\/","url":"https:\/\/learnlearn.uk\/python\/python-console-colors\/","name":"Python Console Colors - Python","isPartOf":{"@id":"https:\/\/learnlearn.uk\/python\/#website"},"datePublished":"2019-06-23T18:37:34+00:00","dateModified":"2023-10-25T10:08:17+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/python\/python-console-colors\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/python\/python-console-colors\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/python\/python-console-colors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Python Unit Home","item":"https:\/\/learnlearn.uk\/python\/"},{"@type":"ListItem","position":2,"name":"Python Console Colors"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/python\/#website","url":"https:\/\/learnlearn.uk\/python\/","name":"Python","description":"Programming","publisher":{"@id":"https:\/\/learnlearn.uk\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/python\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/python\/#organization","name":"Python","url":"https:\/\/learnlearn.uk\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Python"},"image":{"@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/python\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"How to add colored text and background to your Python Programs In this video tutorial you will learn how to add colors to your Python programs. You can install 3rd party modules like colorama, but sometimes it&#8217;s better if you just write your own! Video Tutorial Youtube blocked in your school? Here&#8217;s the Google Drive&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1147"}],"collection":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/comments?post=1147"}],"version-history":[{"count":4,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1147\/revisions"}],"predecessor-version":[{"id":1474,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1147\/revisions\/1474"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/media?parent=1147"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}