concat()#
relationalai.std.strings
#concat(string1: str|Producer, string2: str|Producer, *args: str|Producer) -> Expression
Concatenates two or more strings. Non-string values produced by any of the arguments are converted to strings for concatenation. Missing values are filtered out. Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
string1 | Producer or Python str object | A string or a producer that produces string values. |
string2 | Producer or Python str object | A string or a producer that produces string values. |
*args | Producer or Python str object | Additional strings or producers that produce string values. |
Returns#
An Expression
object.
Example#
Use concat()
to concatenate one or more strings:
#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=-1) # Non-string last_name
Person.add(id=3).set(first_name="Carol") # Missing last_name
# =======
# EXAMPLE
# =======
with model.rule():
person = Person()
# Set the full_name property to be the concatenation of first_name and last_name,
# separated by a space. Note that concat() filters out missing values, so the
# following only sets the full_name property for Alice and Bob.
person.set(full_name=strings.concat(person.first_name, " ", person.last_name))
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 -1
# 2 3 NaN