{"id":1462,"date":"2022-11-09T20:00:00","date_gmt":"2022-11-09T11:00:00","guid":{"rendered":"https:\/\/python-academia.com\/en\/?p=1462"},"modified":"2022-09-23T15:28:56","modified_gmt":"2022-09-23T06:28:56","slug":"python-print","status":"publish","type":"post","link":"https:\/\/python-academia.com\/en\/python-print\/","title":{"rendered":"[Python] print function [format, without newline, f-strings]"},"content":{"rendered":"\n<p>This article shows <span class=\"st-mymarker-s\">basic usage of print() function<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-st-blocks-midashi-box freebox has-title\" style=\"background-color:#eceff1;border-color:#263238;border-radius:0 5px 5px 5px\"><p class=\"p-free\" style=\"border-color:#263238;font-weight:bold\"><span class=\"p-entry-f\" style=\"color:#ffffff;font-weight:bold;background-color:#263238;border-radius:0 0 5px 0\"><i class=\"st-fa st-svg-file-text-o st-css-no\" aria-hidden=\"\"><\/i>Contents<\/span><\/p><div class=\"free-inbox\">\n<p><\/p>\n\n\n\n<ul><li>Basics of print function<\/li><li>Arguments of print function<\/li><li>How to display without line newline<\/li><li>How to use format method<\/li><li>How to use f-strings<\/li><\/ul>\n\n\n\n<p><\/p>\n<\/div><\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Basics of print function<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">Numbers and strings can be displayed by using print function<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>print(\"Apple\")\n# Apple<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Difference between Python2 and Python3<\/h3>\n\n\n\n<p>Print function is written differently in python2 and python3.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\">print &#8220;Apple&#8221;   #python2<br><br>print(&#8220;Apple&#8221;)   #python3<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>In Python 2, it was a &#8220;print statement&#8221;, but it was changed to a &#8220;print function&#8221; in Python 3.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Arguments of print function<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">arguments of print function<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Following code dares to show optional arguments.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import sys\nprint(\"Apple\",\"Banana\", sep=' ', end='\\n', file=sys.stdout, flush=False)\n#Apple Banana<\/code><\/pre>\n\n\n\n<p>With this notation, error will occur if &#8220;sys&#8221; is not imported.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Each argument is briefly explained below.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">sep = &#8216; &#8216; : delimiter<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The &#8220;sep&#8221; parameter is used to modify the delimiter character<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>print(\"Apple\", \"Banana\", \"Orange\") #delimiter : \" \" space\n#Apple Banana Orange\n\nprint(\"Apple\", \"Banana\", \"Orange\", sep=',') #delimiter : \", \" comma\n#Apple,Banana,Orange\n\nprint(\"Apple\", \"Banana\", \"Orange\", sep=', ') #delimiter : \",  \" comma space\n#Apple, Banana, Orange<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">end = &#8216;\\n&#8217; : trailing character<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The &#8220;end&#8221; parameter is used to modify the trailing character<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>print(\"Apple\", \"Banana\") #trailing character : \"\\n\"\nprint(\"Orange\", \"Lemon\") #trailing character : \"\\n\"\n#Apple Banana\n#Orange Lemon\n\nprint(\"Apple\", \"Banana\", end = ',') #trailing character : \", \" comma\nprint(\"Orange\", \"Lemon\", end = ',') #trailing character : \", \" comma\n#Apple Banana,Orange Lemon,<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">file = sys.stdout : save location<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The &#8220;file&#8221; parameter is used to modify the trailing character<\/span>.<\/p>\n\n\n\n<p>The default &#8220;sys.stdout&#8221; is standard output.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The file parameter must be an object with a write method.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output of print function can also be saved to a file.<\/p>\n\n\n\n<p>In the example below, strings are output to a text file using print() function.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>import os\n\ndir_test = r'C:\\Users\\info\\OneDrive\\Desktop'\nname_test = 'output.txt'\npath_test = os.path.join(dir_test, name_test)\n\nfile_test = open(path_test, 'w')\nprint(\"Apple\", \"Banana\", file=file_test)\nfile_test.close()<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The text file is below.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers\"><code>### output.txt\nApple Banana<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">flush=False : flash operation<\/h3>\n\n\n\n<p><span class=\"st-mymarker-s\">The &#8220;file&#8221; parameter is used to modify the trailing character<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If the print function is executed during heavy processing, the output of the print function may be displayed at once after the processing is finished.<\/p>\n\n\n\n<p>If &#8220;flush=True&#8221;, flush operation is enabled and the output of print function can be displayed immediately.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>### Example of displaying processing progress\nimport time\nfor _ in range(10):\n\n    ### --- Insert process here --- ###\n\n    print(\"*\", end=\"\", flush=True)\n    time.sleep(0.5)\n\n#**********<\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output of print function is displayed as processing progresses.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">How to display without newline<\/h2>\n\n\n\n<p>I show <span class=\"st-mymarker-s\">how to display the output of print function without newline<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Change the &#8220;end&#8221; parameter to display output without newline.<\/p>\n\n\n\n<p>In the example below, the trailing character is changed from &#8220;\\n&#8221; to &#8221; &#8220;.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>print(\"Apple\", \"Banana\", end = ' ') #trailing character : \" \" space\nprint(\"Orange\", \"Lemon\", end = ' ') #trailing character : \" \" space\n\n#Apple Banana Orange Lemon<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n\n<table>\n<tbody>\n<tr>\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleA -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"1820454727\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n\n<td>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-doubleB -->\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:336px;height:300px\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"3395043238\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">How to use &#8220;Format method&#8221;<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">&#8220;<\/span><span class=\"st-mymarker-s\">F<\/span><span class=\"st-mymarker-s\">ormat method&#8221; can be used to display variables embedded in string<\/span>.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&#8220;Format method&#8221; is described as follow.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\">print( &#8221; string { } &#8221; .format( variable ) )<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>{ } is replaced by a variable.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>x = 123\nprint('x : {}'.format(x))\n#x : 123<\/code><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If there are multiple variables, separate them with &#8220;,&#8221;.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>x = 123\ny = 456\nprint('x : {}, y : {}'.format(x, y))\n#x : 123, y : 456<\/code><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If index number is specified for { }, it is replaced by the variable corresponding to index number.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>x = \"Variable\"\ny = 123\nz = 456\n\nprint('{0} x : {1}, {0} y : {2}'.format(x, y, z) )\n#Variable x : 123, Variable y : 456<\/code><\/pre>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>If string is specified for { }, it is replaced by the variable corresponding to keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>a = \"Apple\"\nb = \"Banana\"\n\nprint(\"{red} and {yellow}\".format(red = a, yellow = b))\n# Apple and Banana<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<!-- py-article-display -->\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"6239864271\"\n     data-ad-format=\"auto\"\n     data-full-width-responsive=\"true\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">How to use f-strings<\/h2>\n\n\n\n<p><span class=\"st-mymarker-s\">Since Python 3.6, &#8220;f-strings&#8221; have been available<\/span>.<\/p>\n\n\n\n<p>It is a more concise way of writing than &#8220;Format method&#8221;.<\/p>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&#8220;f string&#8221; can be used by writing &#8220;f&#8221; or &#8220;F&#8221;.<\/p>\n\n\n\n<p class=\"is-style-st-paragraph-memo\">print( f&#8221;string { variable }&#8221; )<\/p>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code line-numbers language-Python\"><code>a = \"Apple\"\nb = \"Banana\"\n\nprint(f\"{a} and {b}\")\n\n#Apple and Banana<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p style=\"color:#666;margin-bottom:5px;\">sponsored link<\/p>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6354467409705666\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-matched-content-rows-num=\"4,2\"\n     data-matched-content-columns-num=\"1,4\"\n     data-matched-content-ui-type=\"image_stacked,image_stacked\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-6354467409705666\"\n     data-ad-slot=\"2243394422\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>This article shows basic usage of print() function. Basics of print function Numbers and strings can &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1611,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,9],"tags":[28,16],"_links":{"self":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1462"}],"collection":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/comments?post=1462"}],"version-history":[{"count":117,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1462\/revisions"}],"predecessor-version":[{"id":1658,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/posts\/1462\/revisions\/1658"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media\/1611"}],"wp:attachment":[{"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/media?parent=1462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/categories?post=1462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-academia.com\/en\/wp-json\/wp\/v2\/tags?post=1462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}