{"id":5711,"date":"2023-10-28T11:19:24","date_gmt":"2023-10-28T05:49:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=5711"},"modified":"2023-11-10T12:03:39","modified_gmt":"2023-11-10T06:33:39","slug":"python-how-to-pad-strings-with-zero-space-or-some-other-character","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-pad-strings-with-zero-space-or-some-other-character\/","title":{"rendered":"Python: How to pad strings with zero, space or some other character?"},"content":{"rendered":"
In this tutorial, we are going to discuss how to do padding of strings using zero, space, or some other character. Also, you can get complete knowledge of the methods used for padding strings in Python. So, let’s get started on learning How to pad strings with Zero, Space, or some other character.<\/p>\n
There are two types of padding:<\/p>\n
Left padding means adding a given character on the left side of a string.<\/p>\n
For Example – If we pad three zeros in string 6 i.e, ‘0006’, or if we pad three space in string i.e,’6′<\/p>\n
numStr = \"6\"\r\nprint('Original String :', numStr)\r\n# Left pad the string with 0 to make it's length 4\r\nnumStr = numStr.zfill(4)\r\nprint('Updated String :' , numStr)<\/pre>\nOutput:<\/strong><\/p>\n
Original String : 6\r\nUpdated String : 0006\r\n<\/pre>\nSo in the above example, we have used
'string.zfill(s, width)'<\/code>to pad a given string on the left with zeros (0).<\/p>\n
Also Check:<\/span><\/p>\n
\n
- Convert integer to string in Python<\/span><\/a><\/li>\n
- Convert Python String to Int<\/a><\/li>\n<\/ul>\n
<\/a>Left pad a string with space using string.rjust()<\/h3>\n
In this, we are going to use string.rjust() to add space in a given string.<\/p>\n
numStr = \"6\"\r\nprint('Original String :', numStr)\r\n# Make string right justified of length 4 by padding 3 spaces to left\r\nnumStr = numStr.rjust(4, ' ')\r\nprint('Updated String :', numStr)<\/pre>\nOutput:<\/strong><\/p>\n
Original String : 6\r\nUpdated String : 6\r\n<\/pre>\n<\/a>Left pad a string with some character using string.rjust()<\/h3>\n
For adding any character to our string we will use string.rjust() and pass that character in it.<\/p>\n
numStr = \"6\"\r\nprint('Original String :', numStr)\r\n# Make string right justified of length 4 by padding 3 '-' to left\r\nnumStr = numStr.rjust(4, '-')\r\nprint('Updated String :', numStr)<\/pre>\nOutput:<\/strong><\/p>\n
Original String : 6\r\nUpdated String : ---6\r\n<\/pre>\n<\/a>2. Right padding of string in Python<\/h2>\n
Right padding means adding a given character on the right side of the string.<\/p>\n
Example- If we have a number string i.e. \u201cram\u201d. Now we want to convert this string of length 3 to a string of length 6 by,<\/p>\n
\n
- Right padding three zeros to the string i.e. \u201cram000\u201d<\/li>\n
- Right padding three-space to the string i.e. \u201cram \u201c<\/li>\n
- Right padding three characters to the string i.e. \u201cram\u2014\u201c<\/li>\n<\/ul>\n
<\/a>Right pad a string with zeros using string.ljust()<\/h3>\n
string.ljust() is used for padding a given character to the right of string to make its a length equal to the given width.<\/p>\n
numStr = \"67\"\r\nprint('Original String :', numStr)\r\n# Make string left justified of length 5 by padding 3 0s to the right of it\r\nnumStr = numStr.ljust(5, '0')\r\nprint('Updated String :', numStr)\r\n<\/pre>\nOutput:<\/strong><\/p>\n
Original String : 67\r\nUpdated String : 67000\r\n\r\n<\/pre>\n<\/a>Right pad a string with space using string.ljust()<\/h3>\n
In this, we will use string.ljust() to pad given character,<\/p>\n
userName = \"Shikha\"\r\nprint('Original String :', userName)\r\n# Make string left justified of length 7 by padding 3 spaces to the right of it\r\nuserName = userName.ljust(7, ' ')\r\nprint('Updated String :' , userName, 'is')<\/pre>\nHere 3 spaces are padded to the right of the given string to make its length 9.<\/p>\n