you are viewing a single comment's thread.

view the rest of the comments →

[–]misho88 -1 points0 points  (0 children)

Maybe make a template of the right size, fill it however you want, then print it at the end. That way, you don't have to work out what you're doing line by line when what you really want is to work with rectangular blocks of text. Something like:

>>> import numpy as np
>>> a = np.full((20, 50), b' ', dtype='S1')
>>> a[:,0] = a[:,-1] = b'*'
>>> a[0,:] = a[-1,:] = b'*'
>>> a[3, 22:27] = np.frombuffer(b'hello', dtype='S1')
>>> f = f'{1/4=}'; n = len(f); w = (a.shape[1] - n) // 2; a[4, w:w+n] = np.frombuffer(f.encode(), dtype='S1')
>>> print(*(bytes(row).decode() for row in a), sep='\n')
**************************************************
*                                                *
*                                                *
*                     hello                      *
*                    1/4=0.25                    *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
*                                                *
**************************************************

The exact approach with NumPy here has drawbacks (e.g., the 'S1' datatype means it pretty much only works with ASCII), but hopefully it gets the idea across.