Match the end of a string
Check the letters at the end of the text. The result is true only when those letters match.
String.endswith
Simple example
Does abc end with c?
Start'abc' · 'c'
ResultTrue
def main() raises:
print(String("abc").endswith("c"))
True
Advertisements
Everyday examples
A teacher wants PDF files. Does this name end in .pdf?
Start'notes.pdf' · '.pdf'
ResultTrue
def main() raises:
print(String("notes.pdf").endswith(".pdf"))
True
A music player lists MP3 files. Does the recording have that ending?
Start'rehearsal.wav' · '.mp3'
ResultFalse
def main() raises:
print(String("rehearsal.wav").endswith(".mp3"))
False
Fragile shelf labels end in -F. Does this one belong there?
Start'12-F' · '-F'
ResultTrue
def main() raises:
print(String("12-F").endswith("-F"))
True