{"id":936,"date":"2024-05-12T13:01:25","date_gmt":"2024-05-12T13:01:25","guid":{"rendered":"https:\/\/bookwormhub.com\/blog\/?p=936"},"modified":"2024-05-12T13:01:39","modified_gmt":"2024-05-12T13:01:39","slug":"understanding-the-print-function-in-python","status":"publish","type":"post","link":"https:\/\/bookwormhub.com\/blog\/understanding-the-print-function-in-python\/","title":{"rendered":"Understanding the Print Function in Python"},"content":{"rendered":"<span style=\"font-weight: 400;\">One of the first function you\u2019ll be introduced to when learning Python is the print() function. This article will cover how printing in Python works from the basics along with a few tricks you\u2019d be interested to use in your projects. This function is commonly used in conjunction with python input techniques, and this article is the best guide to string formatting in python.<\/span>\r\n\r\n<!--more-->\r\n<h2><span style=\"font-weight: 400;\">The Basics of Print in Python<\/span><\/h2>\r\n<span style=\"font-weight: 400;\">Given a string in python, the print function displays it in the console as an output. The function accepts not one, not two, but as many strings as you like to print in the console. Let\u2019s work on an example: the task is to print \u201cHello world\u201d. There are two ways to achieve this\u2014either print one string \u201cHello world\u201d, or two strings at the same time\u2014\u201cHello\u201d and \u201cworld\u201d.<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-942\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/1-1-685x300.png\" alt=\"\" width=\"685\" height=\"300\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/1-1-685x300.png 685w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/1-1-768x336.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/1-1.png 781w\" sizes=\"auto, (max-width: 685px) 100vw, 685px\" \/>\r\n<h3><span style=\"font-weight: 400;\">Syntax and Parameters<\/span><\/h3>\r\n<span style=\"font-weight: 400;\">To understand the print function basics, below is the syntax in python of the print function. It consists of 4 optional parameters\u2014sep, end, file, and flush:<\/span>\r\n<pre><b>print<\/b><span style=\"font-weight: 400;\">(<\/span><i><span style=\"font-weight: 400;\">*objects<\/span><\/i><span style=\"font-weight: 400;\">,\u00a0<\/span><i><span style=\"font-weight: 400;\">sep='\u00a0'<\/span><\/i><span style=\"font-weight: 400;\">,\u00a0<\/span><i><span style=\"font-weight: 400;\">end='\\n'<\/span><\/i><span style=\"font-weight: 400;\">,\u00a0<\/span><i><span style=\"font-weight: 400;\">file=None<\/span><\/i><span style=\"font-weight: 400;\">,\u00a0<\/span><i><span style=\"font-weight: 400;\">flush=False<\/span><\/i><span style=\"font-weight: 400;\">)<\/span><\/pre>\r\n<span style=\"font-weight: 400;\">The <\/span><i><span style=\"font-weight: 400;\">*objects<\/span><\/i><span style=\"font-weight: 400;\"> parameter takes in a string (or multiple strings) as we discussed above and is the primary parameter for printing strings. The optional <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\"> parameter specifies the string that is used to separate each of the <\/span><i><span style=\"font-weight: 400;\">object<\/span><\/i><span style=\"font-weight: 400;\"> strings passed (this is of course only relevant if we are printing multiple strings at once!). By default, the value of <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\"> is simply a space character, which is why our output for <\/span><i><span style=\"font-weight: 400;\">print(\u201cHello\u201d, \u201cworld\u201d)<\/span><\/i><span style=\"font-weight: 400;\"> above was \u201cHello world\u201d (instead of \u201cHelloworld\u201d). The <\/span><i><span style=\"font-weight: 400;\">end<\/span><\/i><span style=\"font-weight: 400;\"> parameter specifies the string to print at the end of the print statement. By default, the <\/span><i><span style=\"font-weight: 400;\">end<\/span><\/i><span style=\"font-weight: 400;\"> parameter is \u2018\\n\u2019 which is the line-break character. The <\/span><i><span style=\"font-weight: 400;\">file<\/span><\/i><span style=\"font-weight: 400;\"> parameter specifies a file object to write the print output to. By default, the <\/span><i><span style=\"font-weight: 400;\">file<\/span><\/i><span style=\"font-weight: 400;\"> parameter (if <\/span><i><span style=\"font-weight: 400;\">None<\/span><\/i><span style=\"font-weight: 400;\">) is set to <\/span><i><span style=\"font-weight: 400;\">sys.stdout<\/span><\/i><span style=\"font-weight: 400;\"> (i.e., standard system output\u2014which is your console!). The <\/span><i><span style=\"font-weight: 400;\">flush<\/span><\/i><span style=\"font-weight: 400;\"> parameter is a rather advanced topic for most, however to simply put\u2014output to console or a file is normally buffered (i.e., not immediately written) for performance reasons. This feature may be toggled by specifying the <\/span><i><span style=\"font-weight: 400;\">flush<\/span><\/i><span style=\"font-weight: 400;\"> parameter.<\/span>\r\n\r\n&nbsp;\r\n<h2><span style=\"font-weight: 400;\">Customizing Print Output<\/span><\/h2>\r\n<span style=\"font-weight: 400;\">To customize the output of print function, we will focus on the first 3 parameters\u2014<\/span><i><span style=\"font-weight: 400;\">*objects<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\">, and <\/span><i><span style=\"font-weight: 400;\">end<\/span><\/i><span style=\"font-weight: 400;\">. Setting <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\"> to \u2018\\n\u2019 (i.e., line break) lets us print a series of strings in different lines\u2014all using just one print function-call. Below is an example of using the custom separator (\u2018\\n\u2019).<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-944\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/2-1.png\" alt=\"\" width=\"805\" height=\"240\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/2-1.png 805w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/2-1-768x229.png 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">Likewise, a customized <\/span><i><span style=\"font-weight: 400;\">end<\/span><\/i><span style=\"font-weight: 400;\"> parameter may help when printing punctuated sentences. Combining the two, we can print a list of strings nicely punctuated all in one print function call and in one line!<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-946\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/3-1.png\" alt=\"\" width=\"856\" height=\"160\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/3-1.png 856w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/3-1-768x144.png 768w\" sizes=\"auto, (max-width: 856px) 100vw, 856px\" \/>\r\n<h3><span style=\"font-weight: 400;\">Working with Strings and Objects<\/span><\/h3>\r\n<span style=\"font-weight: 400;\">Python\u2019s print function goes a step further. The function displays not just strings, but numbers and even complex objects (like dictionaries and lists!). This is something that isn\u2019t readily possible using string concatenation, but definitely something that the print function can undertake.<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-948\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/4-1-754x300.png\" alt=\"\" width=\"754\" height=\"300\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/4-1-754x300.png 754w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/4-1-1024x407.png 1024w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/4-1-768x306.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/4-1.png 1287w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">As observed above, string concatenation fails to combine strings with numbers. However, specifying each string and variable as a parameter to print function quickly solves this issue (you probably want to substitute the variable values with additional python input techniques in the example below!).<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-950\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/5-890x247.png\" alt=\"\" width=\"890\" height=\"247\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/5-890x247.png 890w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/5-1024x285.png 1024w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/5-768x214.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/5.png 1478w\" sizes=\"auto, (max-width: 890px) 100vw, 890px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">Even more powerful functionality is one that allows print function to display Python objects. Yes, you read it correctly\u2014you can define the logic behind how objects are displayed using Python\u2019s <\/span><i><span style=\"font-weight: 400;\">__str__<\/span><\/i><span style=\"font-weight: 400;\"> object overload function as described below:<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-952\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/6-702x300.png\" alt=\"\" width=\"702\" height=\"300\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/6-702x300.png 702w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/6-1024x438.png 1024w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/6-768x328.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/6.png 1429w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">Our implementation looks a lot nicer with this addition. Printing objects had never been simpler. In the example above, we use a functionality known as <\/span><i><span style=\"font-weight: 400;\">f-strings<\/span><\/i><span style=\"font-weight: 400;\"> in Python\u2014a formatted string if you will. This is a method to concatenate strings (incl. numbers!) without the necessity of using type-conversion functions such as str().<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0<\/span>\r\n<h2><span style=\"font-weight: 400;\">Practical Examples of Print Function<\/span><\/h2>\r\n<span style=\"font-weight: 400;\">As we\u2019ve discovered from prior examples, the versatility of the print function allows us to print strings in a much more readable manner. This is especially useful when printing data for logging. Consider the previous scenario where we have a Human class representing a person with the attributes\u2014name, age, and location. Using the __str__ object overload, we define a string representation for class attributes and print log messages for our game server this way:<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-954\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/7-724x300.png\" alt=\"\" width=\"724\" height=\"300\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/7-724x300.png 724w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/7-1024x424.png 1024w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/7-768x318.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/7.png 1398w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">This approach lets us print custom messages incorporating objects (a complex data type!) seamlessly. The output is both\u2014informative, and easy to read. Combining objects and strings eases dynamic creation of text.<\/span>\r\n<h2><span style=\"font-weight: 400;\">Advanced Techniques with Print<\/span><\/h2>\r\n<span style=\"font-weight: 400;\">An often-overlooked parameter of the print function is the <\/span><i><span style=\"font-weight: 400;\">file <\/span><\/i><span style=\"font-weight: 400;\">parameter\u2014a powerful feature that extends the utility of print beyond just the console. This parameter allows writing nicely formatted strings to a file, which is yet another bonus for logging mechanisms.<\/span>\r\n<h3><span style=\"font-weight: 400;\">Redirecting Output<\/span><\/h3>\r\n<span style=\"font-weight: 400;\">The simplest use of the file parameter is to redirect output from the console to a file. To do this, you first need a file object that you can obtain by using Python&#8217;s <\/span><i><span style=\"font-weight: 400;\">open<\/span><\/i><span style=\"font-weight: 400;\"> function.<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-958\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/8-890x121.png\" alt=\"\" width=\"890\" height=\"121\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/8-890x121.png 890w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/8-1024x140.png 1024w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/8-768x105.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/8.png 1480w\" sizes=\"auto, (max-width: 890px) 100vw, 890px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">The example above creates the output.txt file and writes the line \u2018I wrote this line.\u2019 into the file. Here we use the file parameter to specify the program to write to the output.txt file. As a result, no output is printed to the console (the output is <\/span><i><span style=\"font-weight: 400;\">redirected<\/span><\/i><span style=\"font-weight: 400;\">!). The simple yet powerful <\/span><i><span style=\"font-weight: 400;\">file<\/span><\/i><span style=\"font-weight: 400;\"> parameter allows writing CSV (comma-separated values) files and creating a tabular display structure out of existing data. Going back to our persons example, the two person objects may be written to a data.csv file using the <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\"> parameter in conjunction with <\/span><i><span style=\"font-weight: 400;\">file<\/span><\/i><span style=\"font-weight: 400;\"> parameter as follows:<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-956\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/9-890x143.png\" alt=\"\" width=\"890\" height=\"143\" srcset=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/9-890x143.png 890w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/9-1024x165.png 1024w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/9-768x124.png 768w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/9-1536x247.png 1536w, https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/9.png 1552w\" sizes=\"auto, (max-width: 890px) 100vw, 890px\" \/>\r\n\r\n<span style=\"font-weight: 400;\">The following is the result obtained from executing the above Python code\u2014a nice excel-readable data file of our registered persons.<\/span>\r\n\r\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-960\" src=\"https:\/\/bookwormhub.com\/blog\/wp-content\/uploads\/2024\/05\/10.png\" alt=\"\" width=\"576\" height=\"272\" \/>\r\n<h2><span style=\"font-weight: 400;\">F.A.Q<\/span><\/h2>\r\n<h3><span style=\"font-weight: 400;\">How to use the print function in Python?<\/span><\/h3>\r\n<span style=\"font-weight: 400;\">The print function in Python can be used by simply calling the function print() and supplying a string within its parenthesis. The result is that the given string is printed to the console. The function may be used to write to a file (instead of console) by specifying the <\/span><i><span style=\"font-weight: 400;\">file<\/span><\/i><span style=\"font-weight: 400;\"> parameter.<\/span>\r\n<h3><span style=\"font-weight: 400;\">What are the key parameters of the print function?<\/span><\/h3>\r\n<span style=\"font-weight: 400;\">The print function includes 3 key parameters\u2014<\/span><i><span style=\"font-weight: 400;\">*objects<\/span><\/i><span style=\"font-weight: 400;\">, <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\">, and <\/span><i><span style=\"font-weight: 400;\">end<\/span><\/i><span style=\"font-weight: 400;\">, with a 4<\/span><span style=\"font-weight: 400;\">th<\/span><span style=\"font-weight: 400;\"> parameter <\/span><i><span style=\"font-weight: 400;\">file<\/span><\/i><span style=\"font-weight: 400;\"> to specify where to output the text (by default, the console). The <\/span><i><span style=\"font-weight: 400;\">*objects<\/span><\/i><span style=\"font-weight: 400;\"> parameter represents strings, numbers, or objects to be printed. The <\/span><i><span style=\"font-weight: 400;\">sep<\/span><\/i><span style=\"font-weight: 400;\"> parameter determines the string used to separate the objects, and the <\/span><i><span style=\"font-weight: 400;\">end<\/span><\/i><span style=\"font-weight: 400;\"> parameter the string to print after the subsequent objects are printed.<\/span>","protected":false},"excerpt":{"rendered":"One of the first function you\u2019ll be introduced to when learning Python is the print() function. This article will cover how printing in Python works from the basics along with a few tricks you\u2019d be interested to use in your projects. This function is commonly used in conjunction with python input techniques, and this article [&hellip;]","protected":false},"author":1,"featured_media":938,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/comments?post=936"}],"version-history":[{"count":3,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/posts\/936\/revisions\/966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/media\/938"}],"wp:attachment":[{"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bookwormhub.com\/blog\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}