String Padding Function

  • Share this:

Code introduction


This function is used to pad a string with characters at the beginning to make its length reach a specified width. The default padding character is '0' and the default width is 2.


Technology Stack : str.zfill(width), str.rjust(width, fillchar)

Code Type : String operation

Code Difficulty : Intermediate


                
                    
def zfill(number, width=2, fillchar='0'):
    return str(number).zfill(width).rjust(width, fillchar)