Skip to content

relationalai.std.strings.length()

length(string: str|Producer) -> Expression

Gets the length of a string. If string is a Producer, then length() acts as a filter and removes non-string values from the producer. Must be called in a rule or query context.

NameTypeDescription
stringProducer or Python str objectA string or a producer that produces string values.

An Expression object.

Use length() to get the length of a string:

import relationalai as rai
from relationalai.std import alias, strings
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
alice = Person.add(id=1).set(name="Alice")
# Set a multi-valued hobbies property with a mix of string and non-string values.
alice.hobbies.extend(["reading", "swimming", -1])
# =======
# EXAMPLE
# =======
with model.query() as select:
person = Person()
hobby = person.hobbies
response = select(
alias(hobby, "hobby_string"),
alias(strings.length(hobby), "length")
)
print(response.results)
# hobby_string length
# 0 reading 7
# 1 swimming 8