Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

align a text python

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'))
 
PREVIOUS NEXT
Tagged: #align #text #python
ADD COMMENT
Topic
Name
2+5 =