📜  对齐文本python代码示例

📅  最后修改于: 2022-03-11 14:46:58.338000             🧑  作者: Mango

代码示例1
def alignto(x: int = ..., direction: str = ..., message: str = ...):
    if direction == 'left':
        return f"{message:<{x}}"
    elif direction == 'right':
        return f"{message:^{x}}"
    elif direction == 'center':
        return f"{message:>{x}}"
    else:
        raise ValueError('direction must be left, right, or center')
    
# Try it with print(alignto(15, 'left', 'hi'), alignto(15, 'right', 'hi'), alignto(15, 'center', 'hi'))