String Padding Function

  • Share this:

Code introduction


This function is used to pad a string to a specified width with a specified character.


Technology Stack : String manipulation

Code Type : String processing

Code Difficulty : Intermediate


                
                    
def zfill(value, width, fillchar=' '):
    """填充字符串至指定宽度,使用指定的字符填充。
    
    Args:
        value (str): 需要填充的原始字符串。
        width (int): 目标宽度。
        fillchar (str, optional): 填充字符,默认为空格。
    
    Returns:
        str: 填充后的字符串。
    """
    return fillchar * width + value