String Right Alignment with Padding

  • Share this:

Code introduction


Right-aligns a string within a specified width, padding with a specified character.


Technology Stack : String manipulation

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def zfill(string, width, fillchar=' '):
    """
    Right-align a string within a specified width, padding with a specified character.

    Parameters:
    string (str): The string to be aligned.
    width (int): The total width of the string after alignment.
    fillchar (str, optional): The character used to fill the space. Defaults to a space.

    Returns:
    str: The right-aligned string.
    """
    return string.rjust(width, fillchar)