Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ฟังก์ชัน startswith() และ endswith() ใน Python

Python มีเมธอด startswith(string) ในคลาส String เมธอดนี้รับ prefix string ที่ต้องการค้นหา และเรียกบนอ็อบเจ็กต์สตริง ใช้ได้ดังนี้

>>> 'hello world'.startswith('hell')
True
>>> 'hello world'.startswith('nope')
False

เช่นกันมี endswith สำหรับค้นหา suffix ที่ท้ายสตริง

>>> 'hello world'.endswith('orld')
True
>>> 'hello world'.endswith('nope')
False