# xarray.DataArray.str.pad

#### DataArray.str.pad(width, side='left', fillchar=' ')[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/accessor_str.py#L1229-L1327)

Pad strings in the array up to width.

If width or ‘fillchar\` is array-like, they are broadcast
against the array and applied elementwise.

* **Parameters:**
  * **width** ([`int`](https://docs.python.org/3/builtins/functions.html#int) or [array-like](https://numpy.org/doc/stable/glossary.html#term-array_like) of [`int`](https://docs.python.org/3/builtins/functions.html#int)) – Minimum width of resulting string; additional characters will be
    filled with character defined in `fillchar`.
    If array-like, it is broadcast.
  * **side** (`{"left", "right", "both"}`, *default*: `"left"`) – Side from which to fill resulting string.
  * **fillchar** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or [array-like](https://numpy.org/doc/stable/glossary.html#term-array_like) of [`str`](https://docs.python.org/3/builtins/stdtypes.html#str), *default*: `" "`) – Additional character for filling, default is a space.
    If array-like, it is broadcast.
* **Returns:**
  **filled** (`same type as values`) – Array with a minimum number of char in each element.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('006ffe3c-50bd-434c-974d-93c8957b979e','4987aef2-bae7-4e6f-ba8a-484b0562c9fb','af6fe197-b247-4b8b-9eef-c796c7b76da4','../lite/tree/../notebooks/index.html?path=c499fba4_f585_459c_b075_b7e6b50be619.ipynb','None')">Try it in your browser!</button></div>

Pad strings in the array with a single string on the left side.

Define the string in the array.

```pycon
>>> da = xr.DataArray(["PAR184", "TKO65", "NBO9139", "NZ39"], dims="x")
>>> da
<xarray.DataArray (x: 4)> Size: 112B
array(['PAR184', 'TKO65', 'NBO9139', 'NZ39'], dtype='<U7')
Dimensions without coordinates: x
```

Pad the strings

```pycon
>>> filled = da.str.pad(8, side="left", fillchar="0")
>>> filled
<xarray.DataArray (x: 4)> Size: 128B
array(['00PAR184', '000TKO65', '0NBO9139', '0000NZ39'], dtype='<U8')
Dimensions without coordinates: x
```

Pad strings on the right side

```pycon
>>> filled = da.str.pad(8, side="right", fillchar="0")
>>> filled
<xarray.DataArray (x: 4)> Size: 128B
array(['PAR18400', 'TKO65000', 'NBO91390', 'NZ390000'], dtype='<U8')
Dimensions without coordinates: x
```

Pad strings on both sides

```pycon
>>> filled = da.str.pad(8, side="both", fillchar="0")
>>> filled
<xarray.DataArray (x: 4)> Size: 128B
array(['0PAR1840', '0TKO6500', 'NBO91390', '00NZ3900'], dtype='<U8')
Dimensions without coordinates: x
```

Using an array-like width

```pycon
>>> width = xr.DataArray([8, 10], dims="y")
>>> filled = da.str.pad(width, side="left", fillchar="0")
>>> filled
<xarray.DataArray (x: 4, y: 2)> Size: 320B
array([['00PAR184', '0000PAR184'],
       ['000TKO65', '00000TKO65'],
       ['0NBO9139', '000NBO9139'],
       ['0000NZ39', '000000NZ39']], dtype='<U10')
Dimensions without coordinates: x, y
```

Using an array-like value for fillchar

```pycon
>>> fillchar = xr.DataArray(["0", "-"], dims="y")
>>> filled = da.str.pad(8, side="left", fillchar=fillchar)
>>> filled
<xarray.DataArray (x: 4, y: 2)> Size: 256B
array([['00PAR184', '--PAR184'],
       ['000TKO65', '---TKO65'],
       ['0NBO9139', '-NBO9139'],
       ['0000NZ39', '----NZ39']], dtype='<U8')
Dimensions without coordinates: x, y
```

<div id="af6fe197-b247-4b8b-9eef-c796c7b76da4" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('006ffe3c-50bd-434c-974d-93c8957b979e','af6fe197-b247-4b8b-9eef-c796c7b76da4')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('006ffe3c-50bd-434c-974d-93c8957b979e','af6fe197-b247-4b8b-9eef-c796c7b76da4')">Open In Tab</button></div><div id="4987aef2-bae7-4e6f-ba8a-484b0562c9fb" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
