Search
 
SCRIPT & CODE EXAMPLE
 

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'))
Comment

align a text 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: ****

Comment

PREVIOUS NEXT
Code Example
Python :: how to make lowercase text in python 
Python :: how to serach for multiple attributes in xpath selenium python 
Python :: Find the length of a nested list in python 
Python :: delete element from matrix python 
Python :: PyPip pygame 
Python :: python Sort the dictionary based on values 
Python :: stack more system in python 
Python :: loop through list of lists jinja 
Python :: convert sentence to words python 
Python :: stop flask server 
Python :: create_polygon tkinter 
Python :: python return to top of loop 
Python :: compare lists element wise python 
Python :: error: not well-formed (invalid token) 
Python :: standard deviation in python numpy 
Python :: authentication views django 
Python :: Python Time duration in seconds 
Python :: python 2 print in same line 
Python :: df length 
Python :: django model different schema 
Python :: django models 
Python :: python check if string or list 
Python :: Converting (YYYY-MM-DD-HH:MM:SS) date time 
Python :: convert excel to pdf python 
Python :: coinflip 
Python :: import library to stop warnings in jupyter 
Python :: 2d array row and column index 
Python :: double a value in a list python 
Python :: split column values 
Python :: django test imagefield 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =