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

Python มีเมธอด contains สำหรับ substring หรือไม่

Python ไม่มีเมธอด contains แต่ใช้ได้ 2 วิธี:

1. ใช้ keyword in

>> 'ello' in 'hello world'
True
>>> 'no' in 'hello'
False

2. ใช้ find() หา index

คืน -1 ถ้าไม่เจอ

>> 'hello world'.find('ello')
1
>>> 'hello'.find('no')
-1