# otp.string

### *class* string

Bases: [`str`](https://docs.python.org/3/library/stdtypes.html#str)

OneTick data type representing string with length and varstring.
To set string length use `__getitem__`.
If the length is not set then the [`DEFAULT_LENGTH`](#onetick.py.string.DEFAULT_LENGTH) length is used by default.
In this case using `otp.string` is the same as using `str`.
If the length is set to Ellipse it represents varstring. Varstring is used for returning variably sized strings.

#### NOTE
If you try to set value with length x to string[y] and x > y, value will be truncated to y length.

### Examples

Adding new fields with [`otp.string`](#onetick.py.string) type:

```pycon
>>> t = otp.Tick(A=otp.string[32]('HELLO'))
>>> t['B'] = otp.string[128]('WORLD')
>>> t.schema
{'A': string[32], 'B': string[128]}
>>> otp.run(t)
        Time      A      B
0 2003-12-01  HELLO  WORLD
```

Note that class [`otp.string`](#onetick.py.string) is a child of python's `str` class,
so every object passed to constructor is converted to string.
So it may not work as expected in some cases, e.g. when passing [`Operation`](../operation/root.md#onetick.py.Operation) objects
(because their string representation is the name of the column or OneTick expression).
In this case it's better to use direct type conversion to get [`otp.string`](#onetick.py.string) object:

```pycon
>>> t = otp.Tick(X=otp.string[256](t['_SYMBOL_NAME']))
>>> t['Y'] = t['_SYMBOL_NAME'].astype(otp.string[256])
>>> t.schema
{'X': string[256], 'Y': string[256]}
>>> otp.run(t, symbols='AAAA')
        Time             X     Y
0 2003-12-01  _SYMBOL_NAME  AAAA
```

Setting the type of the existing field:

```pycon
>>> t = otp.Tick(A='a')
>>> t = t.table(A=otp.string[10])
>>> t.schema
{'A': string[10]}
```

Example of truncation column value to set string length.

```pycon
>>> t['A'] *= 100
>>> t['B'] = t['A'].str.len()
>>> otp.run(t)
        Time           A   B
0 2003-12-01  aaaaaaaaaa  10
```

Example of string with default length.

```pycon
>>> t = otp.Tick(A='a')
>>> t['A'] *= 100
>>> t['B'] = t['A'].str.len()
>>> otp.run(t)
        Time                                                                 A   B
0 2003-12-01  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  64
```

Setting Ellipsis as length represents varstring.

```pycon
>>> t = otp.Tick(A='a')
>>> t = t.table(A=otp.string[...])
>>> t.schema
{'A': varstring}
```

Varstring length is multiplied.

```pycon
>>> t['A'] *= 65
>>> t['B'] = t['A'].str.len()
>>> otp.run(t)
        Time                                                                  A   B
0 2003-12-01  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  65
```

otp.varstring is a shortcut:

```pycon
>>> t = otp.Tick(A='a')
>>> t = t.table(A=otp.varstring)
>>> t.schema
{'A': varstring}
```

* **Variables:**
  **DEFAULT_LENGTH** ([*int*](https://docs.python.org/3/library/functions.html#int)) -- default length of the string when the length is not specified

#### DEFAULT_LENGTH *= 64*
