join()#
relationalai.std.strings
#join(strings: Sequence[str|Producer], separator: str|Producer) -> Expression
Combines a sequence of strings into a single string using a specified separator. If strings
or separator
is a Producer
, then join()
acts as a filter and removes any non-string values from the sequence. Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
strings | Sequence of str or Producer | The sequence of strings to be joined. |
separator | str or Producer | The separator to use between strings. |
Returns#
An Expression
object.
Example#
Use join()
to combine strings with a specified separator:
#import relationalai as rai
from relationalai.std import strings
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
Person.add(id=1).set(first_name="Alice", last_name="Smith")
Person.add(id=2).set(first_name="Bob", last_name="Jones")
Person.add(id=3).set(first_name="Invalid", last_name=-1) # Non-string last name
# =======
# EXAMPLE
# =======
# Create a full_name property by joining first_name and last_name with a space.
with model.rule():
person = Person()
person.set(full_name=strings.join([person.first_name, person.last_name], " "))
# Since join() filters out non-string values, the full_name property
# is not set for the person with id=3.
with model.query() as select:
person = Person()
response = select(person.id, person.full_name)
print(response.results)
# id full_name
# 0 1 Alice Smith
# 1 2 Bob Jones
# 2 3 NaN