Library reference

Match the start of a string

Check the letters at the start of the text. The result is true only when those letters match.

String.startswith

Simple example

Does abc start with a?

Start'abc' · 'a'
ResultTrue
lesson.mojo
def main() raises:
    print(String("abc").startswith("a"))
Output
True
Advertisements

Everyday examples

Child tickets start with C-. Is this a child ticket?

Start'C-18' · 'C-'
ResultTrue
lesson.mojo
def main() raises:
    print(String("C-18").startswith("C-"))
Output
True

Express parcels start with EX. Should this parcel go in the express tray?

Start'BX42' · 'EX'
ResultFalse
lesson.mojo
def main() raises:
    print(String("BX42").startswith("EX"))
Output
False

Is this shelf in the LAB section?

Start'LAB-3' · 'LAB'
ResultTrue
lesson.mojo
def main() raises:
    print(String("LAB-3").startswith("LAB"))
Output
True
Run the example
Terminal
mojo lesson.mojo
Official documentation