str.contains

ColExpr.str.contains(
pattern: str,
allow_regex: bool = True,
true_if_regex_unsupported: bool = False,
) ColExpr[Bool][source]

Whether the string contains a given pattern or substring.

Parameters:
  • pattern – The pattern or substring to look for.

  • allow_regex – If set to True (the default value), pattern is treated as a regular expression. Otherwise, pattern is treated as a literal string.

  • true_if_regex_unsupported – If set to True, this function always returns True on backends that do not support regex.

Note

If the backend does not support regex, pattern is automatically treated as a literal string, regardless of allow_regex.

Examples

>>> t = pdt.Table(
...     {
...         "a": ["  BCD ", "-- 00", " A^^u", "-O2", ""],
...         "b": ["12431", "transform", "12__*m", "   ", "abbabbabba"],
...     },
...     name="string table",
... )
>>> (
...     t
...     >> mutate(
...         j=t.a.str.contains(" "),
...         k=t.b.str.contains("a"),
...         l=t.b.str.contains(""),
...     )
...     >> show()
... )
Table string table, backend: PolarsImpl
shape: (5, 5)
┌────────┬────────────┬───────┬───────┬──────┐
│ a      ┆ b          ┆ j     ┆ k     ┆ l    │
│ ---    ┆ ---        ┆ ---   ┆ ---   ┆ ---  │
│ str    ┆ str        ┆ bool  ┆ bool  ┆ bool │
╞════════╪════════════╪═══════╪═══════╪══════╡
│   BCD  ┆ 12431      ┆ true  ┆ false ┆ true │
│ -- 00  ┆ transform  ┆ true  ┆ true  ┆ true │
│  A^^u  ┆ 12__*m     ┆ true  ┆ false ┆ true │
│ -O2    ┆            ┆ false ┆ false ┆ true │
│        ┆ abbabbabba ┆ false ┆ true  ┆ true │
└────────┴────────────┴───────┴───────┴──────┘