{"id":7996,"date":"2021-01-25T16:50:44","date_gmt":"2021-01-25T11:20:44","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=7996"},"modified":"2025-03-07T08:24:53","modified_gmt":"2025-03-07T13:24:53","slug":"how-to-print-objects-in-python-using-the-print-function","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/how-to-print-objects-in-python-using-the-print-function\/","title":{"rendered":"How to Print Objects in Python using the Print Function"},"content":{"rendered":"\n<p>In python, you will always need to display output on your console to check the flow of your code. You can do this using the print objects in python 3.&nbsp;<\/p>\n\n\n\n<p>NB: In python 2, \u2018print\u2019 was not a function but a statement. This is one of the major changes from Python 2 to Python 3.&nbsp;<\/p>\n\n\n\n<p>That said, let\u2019s see how to work with the print function in Python 3.&nbsp;<\/p>\n\n\n\n<p>Print Function Syntax<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Docstring:\n<strong>print<\/strong>(value, ..., sep=' ', end='<strong>\\n<\/strong>', file=sys.stdout, flush=False)\n\nPrints the values to a stream, <strong>or<\/strong> to sys.stdout by default.\nOptional keyword arguments:\nfile:&nbsp; a file-like object (stream); defaults to the current sys.stdout.\nsep: &nbsp; string inserted between values, default a space.\nend: &nbsp; string appended after the last value, default a newline.\nflush: whether to forcibly flush the stream.<\/pre>\n\n\n\n<p>As earlier discussed, the print function is used to display object values on your console. From the docstring, you\u2019d find that you can print more than one object at a time. Each object is separated by a comma. Let\u2019s see an example<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#print all 4 objects at once\nprint('Hello', 1, True, 23.4)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>Hello 1 True 23.4<\/p>\n\n\n\n<p>You\u2019d realize that each object was separated by whitespace. You can change this using the sep argument.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the sep argument in the Print Function Python<\/h2>\n\n\n\n<p>With the sep argument, you can change how each object is separated. If we want each object to be separated by a forward slash, we set sep=\u2019\/ \u2019. Let\u2019s see a coding example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#print all 4 objects separated with \/\nprint('Hello', 1, True, 23.4, sep='\/ ')<\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>Hello\/ 1\/ True\/ 23.4<\/p>\n\n\n\n<p>As seen the objects are now separated by a backward slash \/. Note that you can change the argument to whatever character you wish. However, the argument has to be a string.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the end argument in the Print Function Python<\/h2>\n\n\n\n<p>When you call the print function twice or more consecutively, the python interpreter displays the values of the print function on a new line. See the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#print each object individually\nprint('Hello')\nprint(1)\nprint(True)\nprint(23.4)<\/pre>\n\n\n\n<p><strong>Output:<br><\/strong>Hello<br>1<br>True<br>23.4<\/p>\n\n\n\n<p>Notice that each object was printed on a new line. You can change this using the end argument. If we set end=\u2019, \u2018, it means a comma will be added after the values of the print function are printed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">print each object individually<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">print('Hello', end=', ')<br>print(1, end=', ')<br>print(True, end=', ')<br>print(23.4)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>Hello<br>1<br>True<br>23.4<\/p>\n\n\n\n<p>Note that you can change the end argument for every print function called.<br>\u2018\\n\u2019 means new line<br>\u2018\\t\u2019 means tab i.e 8 whitespaces<br>\u2018\u2026\u2019 means ellipses and so on.<\/p>\n\n\n\n<p>Let\u2019s see an example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">print each object individually<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">print('Hello', end='\\n')<br>print(1, end='\\t')<br>print(True, end=', ')<br>print(23.4, end='\u2026')<\/pre>\n\n\n\n<p><strong>Output:<br><\/strong>Hello<br>1 True, 23.4\u2026<\/p>\n\n\n\n<p>As seen in the result, after \u2018Hello\u2019, the next object was printed on a new line. After 1, the next object was printed with 8 whitespaces. After True, a comma was printed. Finally, after 23.4, an ellipse was printed.&nbsp;<\/p>\n\n\n\n<p>These are the two major arguments you should understand when printing objects onto your terminal, the end and sep operator. Now, you know how to use the print function in Python. If you have any questions, feel free to drop them in the comment section and I\u2019d do my best to answer them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In python, you will always need to display output on your console to check the flow of your code. You can do this using the print objects in python 3.&nbsp; NB: In python 2, \u2018print\u2019 was not a function but a statement. This is one of the major changes from Python 2 to Python 3.&nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7998,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-7996","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=7996"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7996\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7998"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=7996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=7996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=7996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}