Python có từ khóa in để tìm xem một chuỗi có phải substring của chuỗi khác không. Ví dụ:
print('ello' in 'hello world')
KẾT QUẢ
True
Nếu bạn cũng cần index đầu tiên của substring, bạn có thể dùng find(substr) để tìm index. Nếu method này trả về -1, nghĩa là substring không tồn tại trong chuỗi. Ví dụ:
print("hello world".find('ello'))
KẾT QUẢ
1
Kiểm tra 'no' có chứa trong string 'Harry Potter: The Goblet of Fire'
print("Harry Potter: The Goblet of Fire".find('no'))
KẾT QUẢ
-1