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.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
string | Producer or Python str object | A string or a producer that produces string values. |
Returns
Section titled “Returns”An Expression
object.
Example
Section titled “Example”Use length()
to get the length of a string:
import relationalai as raifrom 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