str2d.Str2D.expand

Str2D.expand(x: int = 0, y: int = 0, **kwargs) Str2D[source]

The expand method expands the data by adding padding to the top, bottom, left, and right of the data. This is a wrapper around the pad method where we take into account the alignment parameters and adjust the padding accordingly.

Parameters:
  • x (int, optional) – The number of columns to expand, by default 0.

  • y (int, optional) – The number of rows to expand, by default 0.

  • kwargs (dict) – Additional keyword arguments to pass to the pad method.

Returns:

A new Str2D object with the expanded data.

Return type:

Str2D

Examples

Let’s create an instance of Str2D and assign it to the variable a and then expand it.

from str2d import Str2D

a = Str2D('a b c d\ne f g\nh i\nj')

We can expand the data by passing the number of columns and rows to expand. In order to see the expansion, we’ll use a fill value of ‘.’.

a.expand(1, 1, fill='.')
a b c d.
e f g  .
h i    .
j      .
........

Let’s see what happens when the alignment is set to ‘center’ and ‘middle’. We use an x value of 4 and a y value of 2 to add 4 columns and 2 rows.

a = Str2D(
    'a b c d\ne f g  \nh i    \nj      ',
    valign='middle', halign='center'
)

a.expand(4, 2, fill='.')

...........
..a b c d..
..e f g  ..
..h i    ..
..j      ..
...........