String Right-Justification with Zfill Method

  • Share this:

Code introduction


Use the zfill method of the string to pad the string to the specified width, so that it is right-aligned within the specified width. If the length of the string is less than the specified width, it is padded with zeros on the left.


Technology Stack : String

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def zfill(string, width):
    """
    Right-justify a string within a specified width. Pad with zeros on the left if necessary.
    """
    return string.zfill(width)                
              
Tags: