“Sejajarkan Teks Python” Kode Jawaban

Sejajarkan Teks 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'))
Blyxyas

Sejajarkan Teks Python


>>> lower = [70, 79, 88, 97, 106, 115]
>>> upper = [78, 87, 96, 105, 114, 123]
>>> num = [5, 3, 4, 2, 6, 4]
>>> digits = len(str(max(lower + upper)))
>>> digits
3
>>> f = '{0:>%d}-{1:>%d}: {2}' % (digits, digits)
>>> f
'{0:>3}-{1:>3}: {2}'
>>> for i in range(len(num)):
        print(f.format(lower[i], upper[i], '*' * num[i]))

 70- 78: *****
 79- 87: ***
 88- 96: ****
 97-105: **
106-114: ******
115-123: ****

Breakable Batfish

Jawaban yang mirip dengan “Sejajarkan Teks Python”

Pertanyaan yang mirip dengan “Sejajarkan Teks Python”

Lebih banyak jawaban terkait untuk “Sejajarkan Teks Python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya