concat()#
relationalai.std.strings
#concat(string1: str | Producer, string2: str | Producer) -> Expression
Concatenates two strings. Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
string1 | str or Producer | A string or a producer that produces string values. |
string2 | str or Producer | A string or a producer that produces string values. |
Returns#
An Expression
object.
Example#
#import relationalai as rai
from relationalai.std.strings import concat
# Create a model named "people" with a Person type.
model = rai.Model("people")
Person = model.Type("Person")
# Add some people to the model.
with model.rule():
alice = Person.add(first="Alice", last="Smith")
bob = Person.add(first="Bob", last="Jones")
# Concatenate the first and last names of all people.
with model.query() as select:
person = Person()
full_name = concat(person.first, person.last)
response = select(full_name)
print(response.results)
# Output:
# v
# 0 AliceSmith
# 1 BobJones
To add a space between the first and last names, you can concatenate a space string with last name:
#concat = std.strings.concat
with model.query() as select:
person = Person()
full_name = concat(person.first, concat(" ", person.last))
# Alternatively, you could use std.strings.join:
# full_name = join([person.first, person.last], " ")
response = select(full_name)
print(response.results)
# Output:
# v
# 0 Alice Smith
# 1 Bob Jones