String Right Justification with Padding

  • Share this:

Code introduction


Right-justify a string within a given width. Pad with the specified fill character if the string is shorter than the specified width.


Technology Stack : Built-in string method

Code Type : String operation

Code Difficulty :


                
                    
def zfill(string, width=0, fillchar=' '):
    """
    Right-justify a string in a given width. Pad with fillchar if necessary.
    """
    return string.zfill(width)