{"id":2940,"date":"2018-03-14T11:39:35","date_gmt":"2018-03-14T11:39:35","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2940"},"modified":"2018-03-16T08:14:34","modified_gmt":"2018-03-16T08:14:34","slug":"python-2-7-fundamentals-basic-input-output","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/03\/14\/python-2-7-fundamentals-basic-input-output\/","title":{"rendered":"Python 2.7 &#8211; Fundamentals &#8211; Basic Input &#038; Output"},"content":{"rendered":"<h3>The input() function<\/h3>\n<p>Python has numerous GUI libraries which can create complex user interfaces, but within the core language it has a single input() function which takes input from the keyboard. Whatever is typed is stored in a variable. Whatever is entered is interpreted literally. So in other words, if you enter a string without surrounding quotes it will throw an error. Some examples are given below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-3067\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/input1-620x191.png\" alt=\"\" width=\"620\" height=\"191\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/input1-620x191.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/input1-300x92.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/input1.png 630w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>print function without formatting<\/h3>\n<p>As you already know the print function is the main way of showing output. It can be used to show multiple values at a time, either separated by commas or by using concatenation:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3069\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print1.png\" alt=\"\" width=\"619\" height=\"114\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print1.png 619w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print1-300x55.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>But the print statement has more powerful formatting parameters which can be used to show more complex output .<\/p>\n<h3>print function with formatting<\/h3>\n<p>The format operator is used within the print function. The left part of the % operator contains the output string template and the right part contains the values which will be put into the template.<\/p>\n<p>The following character format operators are used:<\/p>\n<ul>\n<li>d, i &#8211; integer<\/li>\n<li>u &#8211; unsigned integer<\/li>\n<li>f &#8211; floating point number as m.ddddd<\/li>\n<li>e &#8211; floating point number in exponential format<\/li>\n<li>g &#8211;\u00a0 Use %e for exponents less than \u22124 or greater than +5, otherwise use %f<\/li>\n<li>c &#8211; single character<\/li>\n<li>s &#8211; any string data<\/li>\n<li>% &#8211; insert the % as a literal<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-3072\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print2-620x466.png\" alt=\"\" width=\"620\" height=\"466\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print2-620x466.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print2-300x226.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print2-326x245.png 326w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print2-80x60.png 80w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print2.png 634w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Formatting operators can also have modifiers. They are put between the % and the modifier character. They can be used individually or together. Modifiers add further formatting control to the basic formatting operators:<\/p>\n<ul>\n<li>number &#8211;\u00a0 set field width to the number of characters specified by the number<\/li>\n<li>\u00a0&#8211; &#8211; left justify the value<\/li>\n<li>+ &#8211; right justify the value<\/li>\n<li>0 &#8211; fill in leading space with zeros<\/li>\n<li>. &#8211; specify number of decimal places<\/li>\n<li>(name)\u00a0 &#8211; get value of key with name from supplied dictionary<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-3076\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print4-620x261.png\" alt=\"\" width=\"620\" height=\"261\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print4-620x261.png 620w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print4-300x126.png 300w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/print4.png 631w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>In the next post we will look at <a href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/16\/python-2-7-fundamentals-control-structures\/\" target=\"_blank\" rel=\"noopener\">Control Structures<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The input() function Python has numerous GUI libraries which can create complex user interfaces, but within the core language it has a single input() function <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/14\/python-2-7-fundamentals-basic-input-output\/\" title=\"Python 2.7 &#8211; Fundamentals &#8211; Basic Input &#038; Output\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2940"}],"version-history":[{"count":11,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2940\/revisions"}],"predecessor-version":[{"id":3095,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2940\/revisions\/3095"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}