{"id":54570,"date":"2023-07-28T18:16:25","date_gmt":"2023-07-28T18:16:25","guid":{"rendered":"https:\/\/www.askpython.com\/?p=54570"},"modified":"2023-07-28T18:16:45","modified_gmt":"2023-07-28T18:16:45","slug":"02d-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/02d-in-python","title":{"rendered":"What does {:02d} mean in Python?"},"content":{"rendered":"\n<p>String formatting is a very essential element of Python programming. Usually, we get strings that are not completely in the format that we want them to be in. Converting the string in the desired format is called string formatting.<\/p>\n\n\n\n<p>To know more about <a href=\"https:\/\/www.askpython.com\/python\/string\/strings-in-python\">Python Strings<\/a>, check out this article.<\/p>\n\n\n\n<p>In this article, we will brush through different ways of string formatting and how {:02d} is related and used in string formatting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to Python<\/h2>\n\n\n\n<p>Python is a very user-friendly language with very easy-to-learn and grasp syntax, it is gaining popularity due to its English-like syntax and endless libraries.<\/p>\n\n\n\n<p>Python has different data types, viz. integers, booleans, strings, and so on. Python has a great mechanism to handle strings and format them, which we will be discussing in detail in further sections.<\/p>\n\n\n\n<p> To understand <a href=\"https:\/\/www.askpython.com\/python\/page\/2\">Python programming <\/a>in detail, check out the linked article.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Need of String Formatting in Python<\/h3>\n\n\n\n<p>Python has great features for string formatting and manipulation. String plays a vital role in a program or project as Python takes input in the form of strings only, and the input is not always ready to be used for processing. Being careless with strings can lead to errors and may even break our code, so it is important to handle strings, convert them to the desired format, and then pass them on to the processing part. <\/p>\n\n\n\n<p>A well-formatted string is always more readable than the unformatted one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding String Formatting<\/h2>\n\n\n\n<p>Let\u2019s understand the process of string formatting in a bit more detail:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Different Formatting Method<\/h3>\n\n\n\n<p>Python provides various methods by which we can have strings in the format that we want them to be in. Some of the methods are shown below:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u2018%\u2019 Formatting<\/h4>\n\n\n\n<p>To use this method, we should start by knowing about the format specifier. Format specifiers are nothing but characters or alphabets that are written along with the \u2018%\u2019 sign to identify the data type. <\/p>\n\n\n\n<p>They are as follows: %s is used for strings, %d for decimals or integers, %f for floats, and various others. To see how this works, we shall directly jump to an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ncity = &quot;Pune&quot;\nday = 2\n\nmessage = &quot;I moved to %s %d days ago.&quot; %(city, day)\n\nprint(message)\n<\/pre><\/div>\n\n\n<p>The above code will insert the variable values in the order of the access specifier in which they appear in the string. The output can be seen as shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"173\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1.png\" alt=\"String Format Type1\" class=\"wp-image-54571\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1.png 774w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1-300x67.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1-768x172.png 768w\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" \/><figcaption class=\"wp-element-caption\">String Formatting Type1<\/figcaption><\/figure>\n\n\n\n<p>This method of string formatting is one of the older methods.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Formatted String Literals<\/h4>\n\n\n\n<p>In this method, we will the fstrings, which are nothing but formatted strings.<br><\/p>\n\n\n\n<p>To implement this, we need to start by writing \u2018f\u2019 at the beginning of the string before the quotations and mentioning the formatting variables where we want them to be placed inside a set of curly braces \u2018{}\u2019.<br><\/p>\n\n\n\n<p>Let\u2019s jump to an example for a better understanding:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ncity = &quot;Pune&quot;\nday = 2\n\nmessage = f&#039;I moved to {city} {day} days ago.&#039;\n\nprint(message)\n<\/pre><\/div>\n\n\n<p>The above code will give the following output:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"774\" height=\"173\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1-1.png\" alt=\"String Format Type2\" class=\"wp-image-54574\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1-1.png 774w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1-1-300x67.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type1-1-768x172.png 768w\" sizes=\"auto, (max-width: 774px) 100vw, 774px\" \/><figcaption class=\"wp-element-caption\">String Formatting Type2<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">&#8216;str.format()\u2019<\/h4>\n\n\n\n<p>This method requires placing empty curly braces as placeholders for the formatting variables, and then towards the end, we need to finish with a \u2018.format()\u2019 function, in which we mention the formatting variable in the order in which we want to place them inside the string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nname = &quot;George&quot;\n\nmarks = &#039;85&#039;\n\nsubject = &#039;Maths&#039;\n\nmessage = &quot;{} got {} marks in {}.&quot;.format(name, marks, subject)\n\nprint(message)\n<\/pre><\/div>\n\n\n<p>Output for the above code is as given below:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"819\" height=\"172\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type3.png\" alt=\"String Format Type3\" class=\"wp-image-54577\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type3.png 819w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type3-300x63.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/string-format-type3-768x161.png 768w\" sizes=\"auto, (max-width: 819px) 100vw, 819px\" \/><figcaption class=\"wp-element-caption\">String Formatting Type3<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages of using Format Specification<\/h3>\n\n\n\n<p>The format specification is very helpful in terms of presenting your data, and you can format it in your desired style and present it to people who might be able to understand the data better.<br><\/p>\n\n\n\n<p>Formatting specification makes the code easier to read and maintain which further boosts productivity by many folds as the efficiency increases. Consistency of data increases as the data flows through the program in a specified format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The \u2018{:02d}\u2019 Format Specification<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding the various parts of \u2018{:02d}\u2019<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Importance of colon(:) in the format specification<\/h4>\n\n\n\n<p>The colon(:) is really important as it is used as a differentiator or a dividing wall between the placeholders and the format specifiers.<\/p>\n\n\n\n<p>Controls the precision of data flow and displays output. It improves consistency and efficiency and makes controlling the presentation of data much easier.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Why do we need \u20180\u2019?<\/h4>\n\n\n\n<p>The zero makes sure that zero-padding is added to the number.<\/p>\n\n\n\n<p> In this context, consider a case where we have a number in which the digits are less than 2, to make sure that the width of the number is consistent, it will add zeros to fill the remaining space so that the number of digits becomes equal to 2.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Why do we need \u20182\u2019?<\/h4>\n\n\n\n<p>Resemble the width of the formatted number or integer. The resulting string must have at least these many digits in it; if it exceeds the limit, then the digits are adjusted to fit within the specified width.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Why do we need \u2018d\u2019?<\/h4>\n\n\n\n<p>\u2018d\u2019 is nothing but a format specifier, as we have seen in one of the above sections. It just shows that this place is occupied by an integer.<\/p>\n\n\n\n<p>Let&#8217;s see an example of {:02d}:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nn1 = 1\nn2 = 23\nn3 = 456\n\nn1 = &quot;{:02d}&quot;.format(n1)\nn2 = &quot;{:02d}&quot;.format(n2)\nn3 = &quot;{:02d}&quot;.format(n3)\n\nprint(n1)\nprint(n2)\nprint(n3)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/format-specification-example-678x1024.png\" alt=\"Format Specification Example\" class=\"wp-image-54584\" width=\"215\" height=\"325\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/format-specification-example-678x1024.png 678w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/format-specification-example-199x300.png 199w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/format-specification-example-768x1160.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/format-specification-example.png 984w\" sizes=\"auto, (max-width: 215px) 100vw, 215px\" \/><figcaption class=\"wp-element-caption\">Format Specification Example<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Zero Padding<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Zero Padding?<\/h3>\n\n\n\n<p>Python doesn\u2019t support integers will leading zeros. In such case, if you want to add zeros, the method that you will use is \u2018Zero Padding\u2019. Zero padding simply adds leading zeros to a number so that it occupies the width that is passed.<\/p>\n\n\n\n<p>As we have seen in the above example, {:02d} adds one leading zero to all the single-digit numbers so that the width of the number is equal to 2. This addition of leading zero to meet the required width is called \u2018Zero Padding\u2019.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of \u2018Zero Padding\u2019<\/h3>\n\n\n\n<p>In the context of {:02d}, we will see how zero padding works on both single and multi-digit integers.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\n#Single Digit\n\nn1 = 9\nn2 = 8\nn3 = 3\n\n#Multi Digit\n\nn4 = 12\nn5 = 345\nn6 = 6789\n\n###Formatting\n\n#SINGLE\nn1 = &quot;{:02d}&quot;.format(n1)\nn2 = &quot;{:02d}&quot;.format(n2)\nn3 = &quot;{:02d}&quot;.format(n3)\n\n#MULTI\n\nn4 = &quot;{:02d}&quot;.format(n4)\nn5 = &quot;{:02d}&quot;.format(n5)\nn6 = &quot;{:02d}&quot;.format(n6)\n\nprint(&quot;Single Digit Format:&quot;)\nprint(n1)\nprint(n2)\nprint(n3)\n\nprint(&quot;Multi-digit Format:&quot;)\nprint(n4)\nprint(n5)\nprint(n6)\n<\/pre><\/div>\n\n\n<p>You can see the difference between single and multi-digit integers in the output as shown in the below image:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/zero-padding-example.png\" alt=\"Zero Padding Example\" class=\"wp-image-54609\" width=\"329\" height=\"248\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/zero-padding-example.png 698w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/zero-padding-example-300x226.png 300w\" sizes=\"auto, (max-width: 329px) 100vw, 329px\" \/><figcaption class=\"wp-element-caption\">Zero Padding Example<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Applications<\/h2>\n\n\n\n<p>After studying the theory concepts, it&#8217;s now time to understand and take a look at how we can use the above-gained knowledge in practical applications.<br><\/p>\n\n\n\n<p>Some examples are given below:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Date Time Formatting<\/h3>\n\n\n\n<p>The {:02d} can be used with other format specifiers to format the date in the desired format.<br>We will be formatting the date in the format DD\/MM\/YYYY format with the help of the format specifiers in the below example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom datetime import datetime as dt\n\ndate = dt(2023, 7, 26)\n\nyear = date.year\nmonth = date.month\nday = date.day\n\n#Get date in DD\/MM\/YYYY format\nnew_date_format = &quot;{:02d}\/{:02d}\/{:04d}&quot;.format(day, month, year)\n\nprint(&quot;Default Format of Date: &quot;,date)\n\nprint(&quot;New format of date: &quot;,new_date_format)\n\n<\/pre><\/div>\n\n\n<p>If the above code is without any errors, it should give this output. You can see how the format specifier helps us in formatting strings very easily.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/date-time-example-1024x170.png\" alt=\"Date Time Example\" class=\"wp-image-54619\" width=\"640\" height=\"106\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/date-time-example-1024x170.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/date-time-example-300x50.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/date-time-example-768x128.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/date-time-example.png 1227w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Date Time Example<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Generating Sequence of Filenames<\/h3>\n\n\n\n<p>We are working on projects that require you to handle a large number of files with the same path and almost the same name, you might have to manually create a list of these, but using a format specifier, we can automate this boring process.<\/p>\n\n\n\n<p>Let us understand this process with a simple example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\n## Generating filenames\n\nnumber_of_files = 5\nprefix = &#039;sample&#039;\nextension = &#039;.doc&#039;\n\nfilenames = &#x5B;&quot;{}{:02d}{}&quot;.format(prefix, i, extension) for i in range(1, number_of_files+1)]\n\nfor file in filenames:\n\tprint(file)\n\n<\/pre><\/div>\n\n\n<p>Running the above code will generate a list of filenames as specified in the parameter, which can be seen in the image below:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"584\" height=\"406\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/filname-generating-example.png\" alt=\"Filname Generating Example\" class=\"wp-image-54625\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/filname-generating-example.png 584w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/07\/filname-generating-example-300x209.png 300w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><figcaption class=\"wp-element-caption\">Filename Generating Example<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices and Tips<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Common Mistakes To Avoid<\/h3>\n\n\n\n<p>People commit common mistakes while using these format specifiers and then struggle to figure out what\u2019s going wrong in the code, so it is important to avoid making these small mistakes. <\/p>\n\n\n\n<p>These could be a missing or misplaced colon (:), maybe omitting the width specifier, or maybe mismatched format specifiers.<\/p>\n\n\n\n<p> To avoid these mistakes, it is strongly recommended to adhere to the syntax and the way of writing that is mentioned in the article above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Formatting Negative Integers<\/h3>\n\n\n\n<p>To use the format specifier for negative integers, we would need to use the \u2018abs()\u2019 method in the earlier versions, but now we can handle the negative integers in the same way as we did with the positive ones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In conclusion, let\u2019s brush through all the concepts that we went through the in this article. We started by understanding Python and its String, and how string formatting is important. <\/p>\n\n\n\n<p>We covered various string formatting methods and techniques and even saw examples of them. We did a proper autopsy of the {:02d} format specifier to understand how it works and what is the relevance of each of its components. <\/p>\n\n\n\n<p>Towards the end, we figured out what zero padding is and, after that, saw some practical applications. <\/p>\n\n\n\n<p>To conclude, there are some best practices and tips mentioned to be followed while using {:02d}.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/string.html\" target=\"_blank\" rel=\"noopener\">Official Documentation<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/36543804\/what-does-02d-mean-in-python#:~:text=02d%20formats%20an%20integer%20(%20d,%3A%2042%2C%203%3A%20151&#039;\" target=\"_blank\" rel=\"noopener\">Stackoverflow Query<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>String formatting is a very essential element of Python programming. Usually, we get strings that are not completely in the format that we want them to be in. Converting the string in the desired format is called string formatting. To know more about Python Strings, check out this article. In this article, we will brush [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":54645,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-54570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/54570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=54570"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/54570\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/54645"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=54570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=54570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=54570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}