Skip to content

String

SUMMARY

A string scalar value.

python
from telekinesis import datatypes
value = datatypes.String("hello")
API Reference
Complete API documentation for String, including parameters, attributes, and methods.
View Reference →

Parameters

ParameterTypeDefaultDescription
datastrRequiredThe value to store. No other type is accepted.

Raises

ExceptionCondition
TypeErrordata is not a str

Attributes

AttributeTypeDescription
datastrThe wrapped value. Since str is immutable, no copying is ever required — reading data returns the normalized value directly. Assigning a new value re-validates it the same way as construction.

Methods

MethodTypeDescription
String.coerce(value, name="value")StringConverts a str into a String. If value is already a String, it is returned unchanged. The optional name is only used to make validation error messages more descriptive.
lower()StringReturns a new String with all the characters lowercased.
upper()StringReturns a new String with all the characters uppercased.
strip(chars=None)StringReturns a new String with leading and trailing characters removed. By default (chars=None) it strips whitespace; pass a set of characters to strip those instead.
split(sep=None, maxsplit=-1)list[String]Splits into a list of pieces on sep. When sep is None (the default), it splits on runs of whitespace. maxsplit caps the number of splits; -1 (the default) means no limit.

Operators

OperationReturns
str(s)str
bool(s)bool
len(s)int
s + tString
s * nString
s[i]String
s[i:j]String
x in sbool
s == tbool
s != tbool
s < tbool
s <= tbool
s > tbool
s >= tbool

+ and in accept a String or native str on either side. * accepts an Int or native int repeat count (bool rejected). f-string formatting delegates to the wrapped str (e.g. f"{String('hi'):>5}"" hi").

Visualization

python
import rerun as rr

# Your code block
# ....

rr.init("string_example", spawn=True)
datatypes.visualize(value, entity_path="/value", label="String")

Example

python
"""Demonstrates the Telekinesis String datatype."""

import time

import rerun as rr
from loguru import logger

from telekinesis import datatypes

def string_example():
    """Demonstrate creation, inspection, operations, visualization, and serialization."""

    # ======================= Create ============================================
    value = datatypes.String("Hello")
    logger.info(f"Created String: {value}")

    coerced = datatypes.String.coerce("Coerced")
    logger.info(f"String coerced from str: {coerced}")

    # ======================= Inspect ===========================================
    logger.info(f"data={value.data}")

    # ======================= Operations ========================================
    value.data = "Hello World"
    logger.info(f"Updated String: {value}")

    other = datatypes.String("!")

    logger.info(f"Concatenation: {value} + {other} = {value + other}")
    logger.info(f"Reflected concatenation: 'Say ' + {value} = {'Say ' + value}")
    logger.info(f"Repeat: {value} * 3 = {value * 3}")
    logger.info(f"Reflected repeat: 3 * {value} = {3 * value}")
    logger.info(f"Lower: {value}.lower() = {value.lower()}")
    logger.info(f"Upper: {value}.upper() = {value.upper()}")

    padded = datatypes.String("  padded  ")
    logger.info(f"Strip: {padded!r}.strip() = {padded.strip()}")

    topic = datatypes.String("/topic/ns/name")
    logger.info(f"Split: {topic}.split('/') = {topic.split('/')}")

    logger.info(f"Length: len({value}) = {len(value)}")
    logger.info(f"Contains: 'World' in {value} = {'World' in value}")
    logger.info(f"Indexing: {value}[0] = {value[0]}")
    logger.info(f"Slicing: {value}[0:5] = {value[0:5]}")
    logger.info(f"Format: '{value:>20}'")

    logger.info(f"str(value)={str(value)}")
    logger.info(f"bool(value)={bool(value)}")

    logger.info(f"EQ: {value} == {other} = {value == other}")
    logger.info(f"LT: {value} < {other} = {value < other}")
    logger.info(f"LE: {value} <= {other} = {value <= other}")
    logger.info(f"GT: {value} > {other} = {value > other}")
    logger.info(f"GE: {value} >= {other} = {value >= other}")

    # ======================= Visualize =========================================
    rr.init("string_example", spawn=True)
    datatypes.visualize(value, entity_path="/string")

    # ======================= Serialize / Deserialize ===========================
    start = time.perf_counter()
    serialized = datatypes.serialize(value)
    serialization_ms = (time.perf_counter() - start) * 1000

    start = time.perf_counter()
    deserialized = datatypes.deserialize(serialized)["param_0"]
    deserialization_ms = (time.perf_counter() - start) * 1000

    logger.info(f"Deserialized String: {deserialized}")
    logger.info(f"Round-trip successful: {value == deserialized}")
    logger.info(f"Serialization time: {serialization_ms:.3f} ms")
    logger.info(f"Deserialization time: {deserialization_ms:.3f} ms")


if __name__ == "__main__":
    string_example()