Skip to content

relationalai.std.strings.join()

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.

NameTypeDescription
stringsSequence of str or ProducerThe sequence of strings to be joined.
separatorstr or ProducerThe separator to use between strings.

An Expression object.

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