# Chalk Python SDK Reference: String Functions
source: https://docs.chalk.ai/api-docs#section-string-functions

## `chr(code)`
source: https://docs.chalk.ai/api-docs#chr

Convert Unicode code point to character.

**Parameters**

- `code`: `Underscore | Any`
  The Unicode code point as integer.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Unicode:
   id: str
   code_point: int
   character: str = F.chr(_.code_point)
```

## `ends_with(expr, suffix)`
source: https://docs.chalk.ai/api-docs#ends_with

Evaluates if the string ends with the specified suffix.

**Parameters**

- `expr`: `Underscore | Any`
  The string to check against the suffix.
- `suffix`: `Underscore | Any`
  The suffix or feature to check if the string ends with.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   category: str
   is_food: bool = F.ends_with(_.name, "Food")
```

## `jaccard_similarity(a, b)`
source: https://docs.chalk.ai/api-docs#jaccard_similarity

Compute the Jaccard similarity, character by character, between two strings. Returns a float in the range `[0, 1]`.

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.jaccard_similarity(_.name, _.email)
```

## `jaro_winkler_distance(a, b, prefix_weight)`
source: https://docs.chalk.ai/api-docs#jaro_winkler_distance

Compute the Jaro-Winkler distance between two strings.

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.
- `prefix_weight`: `float` = `0.1`
  The prefix weight parameter for the distance calculation. Should be between `0.0` and `0.25`. `0.1` by default.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.jaro_winkler_distance(_.name, _.email)
```

## `levenshtein_distance(a, b)`
source: https://docs.chalk.ai/api-docs#levenshtein_distance

Compute the Levenshtein distance between two strings.

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.levenshtein_distance(_.name, _.email)
```

## `length(expr)`
source: https://docs.chalk.ai/api-docs#length

Compute the length of a string.

**Parameters**

- `expr`: `Underscore | Any`
  The string to compute the length of.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction
   id: str
   category: str
   category_length: int = F.length(_.category)
```

## `like(expr, pattern)`
source: https://docs.chalk.ai/api-docs#like

Evaluates if the string matches the pattern.

Patterns can contain regular characters as well as wildcards. Wildcard characters can be escaped using the single character specified for the escape parameter. Matching is case-sensitive.

Note: The wildcard `%` represents 0, 1 or multiple characters and the wildcard `_` represents exactly one character.

For example, the pattern `John%` will match any string that starts with `John`, such as `John`, `JohnDoe`, `JohnSmith`, etc.

The pattern `John_` will match any string that starts with `John` and is followed by exactly one character, such as `JohnD`, `JohnS`, etc. but not `John`, `JohnDoe`, `JohnSmith`, etc.

**Parameters**

- `expr`: `Underscore | Any`
  The string to check against the pattern.
- `pattern`: `str`
  The pattern to check the string against.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   is_john: bool = F.like(_.name, "John%")
```

## `lower(expr)`
source: https://docs.chalk.ai/api-docs#lower

Convert a string to lowercase.

**Parameters**

- `expr`: `Underscore | Any`
  The string to convert to lowercase.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   normalized: str = F.trim(F.lower(_.name))
```

## `lpad(string, size, padstring)`
source: https://docs.chalk.ai/api-docs#lpad

Left-pad string to specified length with pad string.

**Parameters**

- `string`: `Underscore | Any`
  The string to pad.
- `size`: `Underscore | Any`
  The target length.
- `padstring`: `Underscore | Any`
  The string to pad with.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class AccountID:
   id: str
   account_number: str
   padded_id: str = F.lpad(_.account_number, 10, "0")
```

## `partial_ratio(a, b)`
source: https://docs.chalk.ai/api-docs#partial_ratio

Compute the Fuzzy Wuzzy partial ratio between two strings. Returns a value in the range `[0, 100]`.

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.partial_ratio(_.name, _.email)
```

## `regexp_extract(expr, pattern, group)`
source: https://docs.chalk.ai/api-docs#regexp_extract

Finds the first occurrence of the regular expression pattern in the string and returns the capturing group number group.

**Parameters**

- `expr`: `Underscore | Any`
  The string to check against the pattern.
- `pattern`: `str`
  The regular expression pattern to check the string against.
- `group`: `int`
  The number of the capturing group to extract from the string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class HiddenNumber:
   id: str
   hidden_number: str = "O0OOO",
   number: str = F.regexp_extract(_.time,  r"([0-9]+)", 1)
```

## `regexp_extract_all(expr, pattern, group)`
source: https://docs.chalk.ai/api-docs#regexp_extract_all

Finds all occurrences of the regular expression pattern in string and returns the capturing group number group.

**Parameters**

- `expr`: `Underscore | Any`
  The string to check against the pattern.
- `pattern`: `str`
  The regular expression pattern to check the string against.
- `group`: `int`
  The number of the capturing group to extract from the string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Time:
   id: str
   time: str = "1y 342d 20h 60m 6s",
   processed_time: list[str] = F.regexp_extract_all(_.time, "([0-9]+)([ydhms])", 2)
```

## `regexp_like(expr, pattern)`
source: https://docs.chalk.ai/api-docs#regexp_like

Evaluates the regular expression pattern and determines if it is contained within string.

This function is similar to the [`like`](https://docs.chalk.ai/api-docs#like) function, except that the pattern only needs to be contained within string, rather than needing to match all the string. In other words, this performs a contains operation rather than a match operation. You can match the entire string by anchoring the pattern using `^` and `$`.

**Parameters**

- `expr`: `Underscore | Any`
  The string to check against the pattern.
- `pattern`: `Underscore | str | Any`
  The regular expression pattern to check the string against.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   is_john: bool = F.regexp_like(_.name, "^John.*$")
```

## `regexp_replace(expr, pattern, replacement)`
source: https://docs.chalk.ai/api-docs#regexp_replace

Replaces every instance of `expr` matched by the regular expression pattern in `pattern` with `replacement`. Capturing groups can be referenced in replacement using `$1`, `$2`, etc. for a numbered group or `${name}` for a named group. A dollar sign (`$`) may be included in the replacement by escaping it with a backslash. If a backslash is followed by any character other than a digit or another backslash in the replacement, the preceding backslash will be ignored.

If no replacement is provided, the matched pattern will be removed from the string.

**Parameters**

- `expr`: `Underscore | Any`
  The string to replace the pattern in.
- `pattern`: `str`
  The regular expression pattern to replace.
- `replacement`: `str | None` = `None`
  The string to replace the pattern with.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   normalize_regex: str
   normalized_name: str = F.regexp_replace(_.name, _.normalize_regex, " ")
```

## `regexp_split(expr, pattern)`
source: https://docs.chalk.ai/api-docs#regexp_split

Splits the provided input on a given regular expression pattern. Returns a list of strings.

**Parameters**

- `expr`: `Underscore | Any`
  The string to split against the pattern.
- `pattern`: `str`
  The regular expression pattern to split the string on.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Text:
   id: str
   spacey_string: str = "a      b  c d",
   text_list: list[str] = F.regexp_split(_.spacey_string, "\s+")
```

## `replace(expr, old, new)`
source: https://docs.chalk.ai/api-docs#replace

Replace all occurrences of a substring in a string with another substring.

**Parameters**

- `expr`: `Underscore | Any`
  The string to replace the substring in.
- `old`: `str`
  The substring to replace.
- `new`: `str`
  The substring to replace the old substring with.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   normalized_name: str = F.replace(_.name, " ", "_")
```

## `reverse(expr)`
source: https://docs.chalk.ai/api-docs#reverse

Reverse the order of a string.

**Parameters**

- `expr`: `Underscore | Any`
  The string to reverse.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   reversed_name: str = F.reverse(_.name)
```

## `rpad(string, size, padstring)`
source: https://docs.chalk.ai/api-docs#rpad

Right-pad string to specified length with pad string.

**Parameters**

- `string`: `Underscore | Any`
  The string to pad.
- `size`: `Underscore | Any`
  The target length.
- `padstring`: `Underscore | Any`
  The string to pad with.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class ProductCode:
   id: str
   base_code: str
   formatted_code: str = F.rpad(_.base_code, 15, "-")
```

## `sequence_matcher_ratio(a, b)`
source: https://docs.chalk.ai/api-docs#sequence_matcher_ratio

Measure the similarity of two strings as by Python `difflib`. Equivalent to `difflib.SequenceMatcher(None, a, b).ratio()`. Returns a value in the range [0, 1].

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.sequence_matcher_ratio(_.name, _.email)
```

## `split(expr, delimiter, maxsplit)`
source: https://docs.chalk.ai/api-docs#split

Splits string by delimiter, returning a list of strings. If maxsplit is set, at most maxsplit splits are performed.

**Parameters**

- `expr`: `Any`
  The string to split.
- `delimiter`: `str`
  The delimiter to split the string on.
- `maxsplit`: `int | None` = `None`
  The maximum number of times to split the string. Once the string has been split "maxsplit" times (starting from the left), the rest of the string is left untouched.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class CSVRow:
   id: str
   data: str
   data: list[str] = F.split(_.data, delimiter=",")
```

## `split_part(expr, delimiter, index)`
source: https://docs.chalk.ai/api-docs#split_part

Splits string by delimiter and returns the index'th element (0-indexed). If the index is larger than the number of fields, returns None.

**Parameters**

- `expr`: `Any`
  The string to split.
- `delimiter`: `str`
  The delimiter to split the string on.
- `index`: `int`
  The index of the the split to return.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class CSVRow:
   id: str
   data: str
   first_element: str = F.split_part(_.data, delimiter=",", index=0)
```

## `starts_with(expr, prefix)`
source: https://docs.chalk.ai/api-docs#starts_with

Evaluates if the string starts with the specified prefix.

**Parameters**

- `expr`: `Underscore | Any`
  The string to check against the prefix.
- `prefix`: `Underscore | Any`
  The prefix or feature to check if the string starts with.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   category: str
   is_food: bool = F.starts_with(_.name, "Food")
```

## `strpos(expr, substring)`
source: https://docs.chalk.ai/api-docs#strpos

Returns the position of the first occurrence of substring in string. Returns -1 if substring is not found.

**Parameters**

- `expr`: `Any`
  The string to search for the substring in.
- `substring`: `Any`
  The substring to search for in the string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Email:
  email: str
  username_length: int = F.strpos(_.email, "@")
```

## `strrpos(expr, substring)`
source: https://docs.chalk.ai/api-docs#strrpos

Returns the position of the last occurrence of substring in string. Returns -1 if substring is not found.

**Parameters**

- `expr`: `Any`
  The string to search for the substring in.
- `substring`: `Any`
  The substring to search for in the string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Email:
  email: str
  domain_length: int = F.strrpos(_.email, "@")
```

## `substr(expr, start, length)`
source: https://docs.chalk.ai/api-docs#substr

Extract a substring from a string.

**Parameters**

- `expr`: `Underscore | Any`
  The string to extract the substring from.
- `start`: `int`
  The starting index of the substring (0-indexed).
- `length`: `int | None` = `None`
  The length of the substring. If [`None`](https://docs.python.org/3/library/constants.html#None), the substring will extend to the end of the string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   category: str
   cat_first_three: str = F.substr(_.category, 0, 3)
```

## `token_set_ratio(a, b)`
source: https://docs.chalk.ai/api-docs#token_set_ratio

Compute the Fuzzy Wuzzy token set ratio between two strings. Returns a value in the range `[0, 100]`.

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.token_set_ratio(_.name, _.email)
```

## `token_sort_ratio(a, b)`
source: https://docs.chalk.ai/api-docs#token_sort_ratio

Compute the Fuzzy Wuzzy token sort ratio between two strings. Returns a value in the range `[0, 100]`.

**Parameters**

- `a`: `Underscore | Any`
  The first string.
- `b`: `Underscore | Any`
  The second string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   email: str
   name_email_sim: int = F.token_sort_ratio(_.name, _.email)
```

## `trim(expr)`
source: https://docs.chalk.ai/api-docs#trim

Remove leading and trailing whitespace from a string.

**Parameters**

- `expr`: `Underscore | Any`
  The string to trim.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   trimmed_name: str = F.trim(_.name)
```

## `unidecode_normalize(a)`
source: https://docs.chalk.ai/api-docs#unidecode_normalize

Normalizes an input utf8 string using NFKD normalization. Returns a normalized utf8 string.

**Parameters**

- `a`: `Underscore | Any`
  utf8 string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   input_string: str
   normalized_string: str = F.unidecode_normalize(_.input_string)
```

## `unidecode_to_ascii(a)`
source: https://docs.chalk.ai/api-docs#unidecode_to_ascii

Normalizes and transliterates an input utf8 string to ASCII. Returns a normalized string with only ASCII characters.

**Parameters**

- `a`: `Underscore | Any`
  utf8 string.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   input_string: str
   normalized_ascii_string: str = F.unidecode_to_ascii(_.input_string)
```

## `upper(expr)`
source: https://docs.chalk.ai/api-docs#upper

Convert a string to uppercase.

**Parameters**

- `expr`: `Underscore | Any`
  The string to convert to uppercase.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class Transaction:
   id: str
   category: str
   normalized: str = F.trim(F.upper(_.category))
```

## `word_stem(string)`
source: https://docs.chalk.ai/api-docs#word_stem

Extract the stem of a word using basic stemming rules.

**Parameters**

- `string`: `Underscore | Any`
  The word to stem.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class TextProcessing:
   id: str
   word: str
   stemmed_word: str = F.word_stem(_.word)
```

## `string_to_bytes(expr, encoding)`
source: https://docs.chalk.ai/api-docs#string_to_bytes

Convert a string to bytes using the specified encoding.

**Parameters**

- `expr`: `Any`
  An underscore expression for a feature to a string feature that should be converted to bytes.
- `encoding`: `'utf-8' | 'hex' | 'base64'`
  The encoding to use when converting the string to bytes.

**Examples**

```python
import chalk.functions as F
from chalk.features import _, features
@features
class User:
   id: str
   name: str
   hashed_name: bytes = F.string_to_bytes(_.name, encoding="utf-8")
```
