{"id":2918,"date":"2017-10-16T13:24:52","date_gmt":"2017-10-16T13:24:52","guid":{"rendered":"http:\/\/www.csestack.org\/?p=2918"},"modified":"2019-03-22T13:33:46","modified_gmt":"2019-03-22T08:03:46","slug":"python-padding-number-string","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/python-padding-number-string\/","title":{"rendered":"Python Padding | How to Pad Zeros to Number or String?"},"content":{"rendered":"<p>Many times to represent the data or display it on the output console, we need to pad a number with extra zeros to make it looks consistent.<\/p>\n<p>In this post, I a going to share the simplest two methods for Python padding.<\/p>\n<p>Before\u00a0jumping into the code, let&#8217;s see&#8230;<\/p>\n<p>If you have the serial number from 0 to 999. It looks something like this&#8230;<\/p>\n<pre>0\r\n1\r\n2\r\n.\r\n.\r\n8\r\n9\r\n.\r\n.\r\n25\r\n26\r\n17\r\n.\r\n.\r\n997\r\n998\r\n999<\/pre>\n<p>The number series does not look fancy as the length of the number in the series varies. If we pad the number to make it have the same length (says 3), it looks like this&#8230;<\/p>\n<p>If we pad the number to make it have the same length (says 3), it looks like this&#8230;<\/p>\n<pre>000\r\n001\r\n002\r\n.\r\n.\r\n008\r\n009\r\n.\r\n.\r\n025\r\n026\r\n017\r\n.\r\n.\r\n997\r\n998\r\n999<\/pre>\n<p>From the above two number series list, which one does look good?<\/p>\n<p>I am sure, it&#8217;s Second!<\/p>\n<p>So let&#8217;s see how to pad integer number and string in Python.<\/p>\n<h3 style=\"text-align: center;\">Python Padding:<\/h3>\n<p>There are multiple methods of doing this. Let&#8217;s check some of the simple Python tricks&#8230;<\/p>\n<h4>Method 1: Using zfill()<\/h4>\n<pre class=\"brush: python\">strNum = '7'\r\nprint strNum.zfill(3)<\/pre>\n<p>The zfill is a function associated with the string object. 3 is the expected length of the string after padding.<\/p>\n<h4>Method 2: Using rjust()<\/h4>\n<pre class=\"brush: python\">strNum = '7'\r\nstrNum.rjust(3, '0')<\/pre>\n<p>The rjust() is string inbuilt function in Python. Pass the expected length of the string after padding and character to be used for padding as two parameters of the function.<\/p>\n<p>With this function, you can use any character for padding.<\/p>\n<h3 style=\"text-align: center;\">Output:<\/h3>\n<p>Both the method will give the same output as&#8230;<\/p>\n<pre>007<\/pre>\n<p>&#8220;1&#8221; gives out as a string of &#8220;001&#8221;<\/p>\n<p>&#8220;2&#8221; gives &#8220;002&#8221;, &#8220;99&#8221; gives &#8220;099&#8221;, &#8220;100&#8221; gives &#8220;100&#8221;, &#8220;999&#8221; gives &#8220;999&#8221;<\/p>\n<p><strong>Exception:<\/strong> If the length of the given string is greater than the padding length, it returns the same string.<\/p>\n<p>Above code returns &#8220;7000&#8221; for &#8220;7000&#8221; even if you pass\u00a03 as expected length after padding.<\/p>\n<p>In Python, you can not pad the integer\u00a0number. You have to convert the number into the string. And then pad it.<\/p>\n<p>Conversion of the integer into the\u00a0string is very easy.<\/p>\n<pre class=\"brush: python\">num = 7\r\nstrNum = str(num)<\/pre>\n<p>Now you can make the consistent length for all the numbers in series.<\/p>\n<p>This function will be more helpful if you have a function that manipulates the same length of the input string.<\/p>\n<p>Other Python Tricks:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.csestack.org\/python-program-swap-two-variables-without-using-third-variable\/\">Swap Two Variables without using Third Variable<\/a>\u00a0[in just one line of code]<\/li>\n<li><a href=\"http:\/\/www.csestack.org\/get-permutations-of-string-in-python-itertools\/\" target=\"_blank\" rel=\"noopener noreferrer\">Get all the Permutations of String<\/a><\/li>\n<li><a href=\"http:\/\/www.csestack.org\/reverse-string-in-python-using-extended-slice-syntax\/\" target=\"_blank\" rel=\"noopener noreferrer\">Reverse String in Python<\/a>\u00a0[using Extended Slice Syntax ]<\/li>\n<\/ul>\n<p>This is all about Python padding. There are so many other methods of doing this. To make it simple to use in your code, I have listed the simplest two methods.<\/p>\n<p>If you think as there are other simplest\u00a0ways of solving this problem, comment below. I am sure it will be helpful other Python Programmers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the function for Python padding? How to print the number or string by adding zeroes at left?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,73],"tags":[],"class_list":["post-2918","post","type-post","status-publish","format-standard","hentry","category-code","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Padding | How to Pad Zeros to Number or String?<\/title>\n<meta name=\"description\" content=\"What is the function for Python padding? How to print the number or string by adding zeroes at left?\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/python-padding-number-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Padding | How to Pad Zeros to Number or String?\" \/>\n<meta property=\"og:description\" content=\"What is the function for Python padding? How to print the number or string by adding zeroes at left?\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/python-padding-number-string\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-16T13:24:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-22T08:03:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"Python Padding | How to Pad Zeros to Number or String?\",\"datePublished\":\"2017-10-16T13:24:52+00:00\",\"dateModified\":\"2019-03-22T08:03:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/\"},\"wordCount\":441,\"commentCount\":6,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"articleSection\":[\"Code\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/\",\"name\":\"Python Padding | How to Pad Zeros to Number or String?\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"datePublished\":\"2017-10-16T13:24:52+00:00\",\"dateModified\":\"2019-03-22T08:03:46+00:00\",\"description\":\"What is the function for Python padding? How to print the number or string by adding zeroes at left?\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/python-padding-number-string\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Padding | How to Pad Zeros to Number or String?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Padding | How to Pad Zeros to Number or String?","description":"What is the function for Python padding? How to print the number or string by adding zeroes at left?","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.csestack.org\/python-padding-number-string\/","og_locale":"en_US","og_type":"article","og_title":"Python Padding | How to Pad Zeros to Number or String?","og_description":"What is the function for Python padding? How to print the number or string by adding zeroes at left?","og_url":"https:\/\/www.csestack.org\/python-padding-number-string\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2017-10-16T13:24:52+00:00","article_modified_time":"2019-03-22T08:03:46+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg","type":"image\/jpeg"}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/python-padding-number-string\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/python-padding-number-string\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"Python Padding | How to Pad Zeros to Number or String?","datePublished":"2017-10-16T13:24:52+00:00","dateModified":"2019-03-22T08:03:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/python-padding-number-string\/"},"wordCount":441,"commentCount":6,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"articleSection":["Code","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/python-padding-number-string\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/python-padding-number-string\/","url":"https:\/\/www.csestack.org\/python-padding-number-string\/","name":"Python Padding | How to Pad Zeros to Number or String?","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"datePublished":"2017-10-16T13:24:52+00:00","dateModified":"2019-03-22T08:03:46+00:00","description":"What is the function for Python padding? How to print the number or string by adding zeroes at left?","breadcrumb":{"@id":"https:\/\/www.csestack.org\/python-padding-number-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/python-padding-number-string\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/python-padding-number-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"Python Padding | How to Pad Zeros to Number or String?"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/2918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=2918"}],"version-history":[{"count":10,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/2918\/revisions"}],"predecessor-version":[{"id":4943,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/2918\/revisions\/4943"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=2918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=2918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=2918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}