# xarray.DataArray.str.split

#### DataArray.str.split(dim, sep=None, maxsplit=-1)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/accessor_str.py#L2538-L2652)

Split strings in a DataArray around the given separator/delimiter sep.

Splits the string in the DataArray from the beginning,
at the specified delimiter string.

If sep is array-like, it is broadcast against the array and applied
elementwise.

* **Parameters:**
  * **dim** ([hashable](../user-guide/terminology.html.md#term-name) or [`None`](https://docs.python.org/3/builtins/constants.html#None)) – Name for the dimension to place the results in.
    If None, place the results as list elements in an object DataArray.
  * **sep** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), *default*: [`None`](https://docs.python.org/3/builtins/constants.html#None)) – String to split on. If `None` (the default), split on any whitespace.
    If array-like, it is broadcast.
  * **maxsplit** ([`int`](https://docs.python.org/3/builtins/functions.html#int), *default*: `-1`) – Limit number of splits in output, starting from the beginning.
    If -1 (the default), return all splits.
* **Returns:**
  **splitted** (`same type as values` or `object array`)

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('9d00994c-58a7-4eb4-93d1-8586816f7241','5755cbb7-4b51-4e8f-8276-09c668c544d7','2141e068-8173-4d20-aa24-9eb618d929a9','../lite/tree/../notebooks/index.html?path=8b7420cc_c9f9_43e5_aa2e_8a7ff660dbf0.ipynb','None')">Try it in your browser!</button></div>

Create a string DataArray

```pycon
>>> values = xr.DataArray(
...     [
...         ["abc def", "spam\t\teggs\tswallow", "red_blue"],
...         ["test0\ntest1\ntest2\n\ntest3", "", "abra  ka\nda\tbra"],
...     ],
...     dims=["X", "Y"],
... )
```

Split once and put the results in a new dimension

```pycon
>>> values.str.split(dim="splitted", maxsplit=1)
<xarray.DataArray (X: 2, Y: 3, splitted: 2)> Size: 864B
array([[['abc', 'def'],
        ['spam', 'eggs\tswallow'],
        ['red_blue', '']],

       [['test0', 'test1\ntest2\n\ntest3'],
        ['', ''],
        ['abra', 'ka\nda\tbra']]], dtype='<U18')
Dimensions without coordinates: X, Y, splitted
```

Split as many times as needed and put the results in a new dimension

```pycon
>>> values.str.split(dim="splitted")
<xarray.DataArray (X: 2, Y: 3, splitted: 4)> Size: 768B
array([[['abc', 'def', '', ''],
        ['spam', 'eggs', 'swallow', ''],
        ['red_blue', '', '', '']],

       [['test0', 'test1', 'test2', 'test3'],
        ['', '', '', ''],
        ['abra', 'ka', 'da', 'bra']]], dtype='<U8')
Dimensions without coordinates: X, Y, splitted
```

Split once and put the results in lists

```pycon
>>> values.str.split(dim=None, maxsplit=1)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs\tswallow']),
        list(['red_blue'])],
       [list(['test0', 'test1\ntest2\n\ntest3']), list([]),
        list(['abra', 'ka\nda\tbra'])]], dtype=object)
Dimensions without coordinates: X, Y
```

Split as many times as needed and put the results in a list

```pycon
>>> values.str.split(dim=None)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs', 'swallow']),
        list(['red_blue'])],
       [list(['test0', 'test1', 'test2', 'test3']), list([]),
        list(['abra', 'ka', 'da', 'bra'])]], dtype=object)
Dimensions without coordinates: X, Y
```

Split only on spaces

```pycon
>>> values.str.split(dim="splitted", sep=" ")
<xarray.DataArray (X: 2, Y: 3, splitted: 3)> Size: 2kB
array([[['abc', 'def', ''],
        ['spam\t\teggs\tswallow', '', ''],
        ['red_blue', '', '']],

       [['test0\ntest1\ntest2\n\ntest3', '', ''],
        ['', '', ''],
        ['abra', '', 'ka\nda\tbra']]], dtype='<U24')
Dimensions without coordinates: X, Y, splitted
```

<div id="2141e068-8173-4d20-aa24-9eb618d929a9" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('9d00994c-58a7-4eb4-93d1-8586816f7241','2141e068-8173-4d20-aa24-9eb618d929a9')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('9d00994c-58a7-4eb4-93d1-8586816f7241','2141e068-8173-4d20-aa24-9eb618d929a9')">Open In Tab</button></div><div id="5755cbb7-4b51-4e8f-8276-09c668c544d7" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`DataArray.str.rsplit`](xarray.DataArray.str.rsplit.html.md#xarray.DataArray.str.rsplit), [`str.split`](https://docs.python.org/3/builtins/stdtypes.html#str.split), [`pandas.Series.str.split`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.split.html#pandas.Series.str.split)
