Skip to content

relationalai.std.strings.strip()

strip(string: str|Producer) -> Expression

Removes leading and trailing whitespace from a string. If string is a Producer, then strip() also acts as a filter and removes non-string values from the producer. Must be called in a rule or query context.

NameTypeDescription
stringProducer or Python str objectThe string to remove whitespace from.

An Expression object.

Use strip() to remove leading and trailing whitespace from a string:

import relationalai as rai
from relationalai.std import alias, strings
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
Person.add(id=1).set(raw_name=" Alice")
Person.add(id=2).set(raw_name="Bob ")
Person.add(id=3).set(raw_name=" Carol ")
Person.add(id=4).set(raw_name=-1) # Non-string raw_name
# =======
# EXAMPLE
# =======
# Set a name property equal to raw_name with leading and trailing whitespace removed.
with model.rule():
person = Person()
person.set(name=strings.strip(person.raw_name))
# Since strip() filters out non-string values, the name property is not set for
# the person with id=4.
with model.query() as select:
person = Person()
response = select(person.id, person.name)
print(response.results)
# id name
# 0 1 Alice
# 1 2 Bob
# 2 3 Carol
# 3 4 NaN