Trong bài viết này, chúng tôi sẽ đề cập đến cách kiểm tra xem một chuỗi Python có chứa một chuỗi hoặc chuỗi con khác hay không trong Python. Đưa ra hai chuỗi, kiểm tra xem chuỗi con có tồn tại trong chuỗi đã cho hay không. Show
Example 1: Input : Substring = "geeks" String="geeks for geeks" Output : yes Example 2: Input : Substring = "geek" String="geeks for geeks" Output : yes Python có chuỗi chứa phương thức chuỗi con khôngCó, Kiểm tra chuỗi con là một trong những tác vụ được sử dụng nhiều nhất trong python. Python sử dụng nhiều phương thức để kiểm tra một chuỗi chứa một chuỗi con như find(), index(), count(), v.v. Phương pháp nhanh và hiệu quả nhất là sử dụng toán tử “in” được sử dụng làm toán tử so sánh. Ở đây chúng tôi sẽ đề cập đến các cách tiếp cận khác nhau như
Phương pháp 1. Kiểm tra chuỗi con bằng cách sử dụng if… inPython3Yes! it is present in the string1 Yes! it is present in the string2 Yes! it is present in the string3 Yes! it is present in the string4
Yes! it is present in the string5 Yes! it is present in the string6 Yes! it is present in the string7 Yes! it is present in the string8 Yes! it is present in the string9 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string02 Yes! it is present in the string03 Yes! it is present in the string04 Yes! it is present in the string05 Yes! it is present in the string9 Yes! it is present in the string00____101 Yes! it is present in the string09 Yes! it is present in the string03 Đầu ra Yes! it is present in the string Phương pháp 2. Kiểm tra chuỗi con bằng phương thức split()Kiểm tra xem chuỗi con có trong chuỗi đã cho hay không mà không sử dụng bất kỳ hàm sẵn có nào. Đầu tiên chia chuỗi đã cho thành các từ và lưu trữ chúng trong một biến s sau đó sử dụng điều kiện if, kiểm tra xem chuỗi con có tồn tại trong chuỗi đã cho hay không Python3Yes! it is present in the string21 Yes! it is present in the string22
Yes! it is present in the string23 Yes! it is present in the string24 Yes! it is present in the string3 Yes! it is present in the string26 Yes! it is present in the string27 Yes! it is present in the string28 Yes! it is present in the string3 Yes! it is present in the string10 Yes! it is present in the string11
Yes! it is present in the string12 Yes! it is present in the string13 Yes! it is present in the string3 Yes! it is present in the string15
Yes! it is present in the string16 Yes! it is present in the string17 Yes! it is present in the string5 Yes! it is present in the string28 Yes! it is present in the string7 Yes! it is present in the string21 Yes! it is present in the string9 Yes! it is present in the string00____101 Yes! it is present in the string25 Yes! it is present in the string03 Yes! it is present in the string04 Yes! it is present in the string05 Yes! it is present in the string9 Yes! it is present in the string00____101 Yes! it is present in the string32 Yes! it is present in the string03 Đầu ra Yes! it is present in the string0 Phương pháp 3. Kiểm tra chuỗi con bằng phương thức find()Chúng ta có thể kiểm tra lặp đi lặp lại mọi từ, nhưng Python cung cấp cho chúng ta một hàm sẵn có find() để kiểm tra xem chuỗi con có tồn tại trong chuỗi hay không, việc này được thực hiện trong một dòng. Hàm find() trả về -1 nếu không tìm thấy, ngược lại nó trả về lần xuất hiện đầu tiên, vì vậy sử dụng hàm này vấn đề này có thể được giải quyết. Python3Yes! it is present in the string34 Yes! it is present in the string35
Yes! it is present in the string36 Yes! it is present in the string37 Yes! it is present in the string9 Yes! it is present in the string5 Yes! it is present in the string20 Yes! it is present in the string3 Yes! it is present in the string3 Yes! it is present in the string23 Yes! it is present in the string24 Yes! it is present in the string25 Yes! it is present in the string26 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string29 Yes! it is present in the string03 Yes! it is present in the string9 Yes! it is present in the string04 Yes! it is present in the string05 Yes! it is present in the string26 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string97 Yes! it is present in the string03
Yes! it is present in the string99 Yes! it is present in the string24 Yes! it is present in the string3 Yes! it is present in the string26 Yes! it is present in the string93____13 Yes! it is present in the string95 Yes! it is present in the string96 Đầu ra Yes! it is present in the string2 Phương pháp 4. Kiểm tra chuỗi con bằng phương thức “count()”Bạn cũng có thể đếm số lần xuất hiện của một chuỗi con cụ thể trong một chuỗi, sau đó bạn có thể sử dụng phương thức đếm() của Python. Nếu chuỗi con không được tìm thấy thì “có” sẽ được in ra nếu không thì “không sẽ được in ra” Python3Yes! it is present in the string36 Yes! it is present in the string98 Yes! it is present in the string9 Yes! it is present in the string5 Yes! it is present in the string01 Yes! it is present in the string02 Yes! it is present in the string25 Yes! it is present in the string26 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string97 Yes! it is present in the string03 Yes! it is present in the string9 Yes! it is present in the string04 Yes! it is present in the string05 Yes! it is present in the string26 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string29 Yes! it is present in the string03
Yes! it is present in the string17 Yes! it is present in the string3 Yes! it is present in the string4 Yes! it is present in the string20 Yes! it is present in the string3 Yes! it is present in the string10 Yes! it is present in the string23 Đầu ra Yes! it is present in the string1 Phương pháp 5. Kiểm tra chuỗi con bằng phương thức index()Các. phương thức index() trả về chỉ mục bắt đầu của chuỗi con được truyền dưới dạng tham số. Ở đây "chuỗi con" có mặt ở chỉ số 16 Python3Yes! it is present in the string24 Yes! it is present in the string3 Yes! it is present in the string26 Yes! it is present in the string27 Yes! it is present in the string3 Yes! it is present in the string02 Yes! it is present in the string30 Yes! it is present in the string3 Yes! it is present in the string32 Yes! it is present in the string00 Yes! it is present in the string34 Yes! it is present in the string35 Yes! it is present in the string36 đầu ra Yes! it is present in the string2 Phương pháp 6. Kiểm tra chuỗi con bằng cách sử dụng lớp phép thuật “__contains__”Chuỗi Python __contains__(). Phương thức này dùng để kiểm tra xem chuỗi này có nằm trong chuỗi kia hay không. Python3Yes! it is present in the string37 Yes! it is present in the string3 Yes! it is present in the string39_______140 Yes! it is present in the string41 Yes! it is present in the string42 Yes! it is present in the string41 Yes! it is present in the string44 Yes! it is present in the string41 Yes! it is present in the string46 Yes! it is present in the string47 Yes! it is present in the string48 Yes! it is present in the string49 Yes! it is present in the string7 Yes! it is present in the string51 Yes! it is present in the string9 Yes! it is present in the string5 Yes! it is present in the string54 Yes! it is present in the string55 Yes! it is present in the string25 Yes! it is present in the string26 Yes! it is present in the string00____159 Yes! it is present in the string60 Yes! it is present in the string03 Đầu ra Yes! it is present in the string3 Phương pháp 7. Kiểm tra chuỗi con bằng biểu thức chính quyRegEx có thể được sử dụng để kiểm tra xem một chuỗi có chứa mẫu tìm kiếm đã chỉ định hay không. Python có một gói tích hợp gọi là re, có thể được sử dụng để làm việc với Biểu thức chính quy. Python3Yes! it is present in the string62 Yes! it is present in the string63 Yes! it is present in the string64 Yes! it is present in the string65
Yes! it is present in the string1 Yes! it is present in the string2 Yes! it is present in the string3 Yes! it is present in the string4 Yes! it is present in the string70 Yes! it is present in the string3 Yes! it is present in the string10
Yes! it is present in the string73 Yes! it is present in the string74 Yes! it is present in the string5 Yes! it is present in the string76 Yes! it is present in the string9 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string80 Yes! it is present in the string81 Yes! it is present in the string82 Yes! it is present in the string01 Yes! it is present in the string26 Yes! it is present in the string85 Yes! it is present in the string04 Yes! it is present in the string05 Yes! it is present in the string9 Yes! it is present in the string00 Yes! it is present in the string01 Yes! it is present in the string91 Yes! it is present in the string81 Yes! it is present in the string82 Yes! it is present in the string01 Yes! it is present in the string26 Yes! it is present in the string85 Đầu ra Yes! it is present in the string2 Phương pháp. Sử dụng hiểu danh sáchPython3Yes! it is present in the string13 Yes! it is present in the string3 Yes! it is present in the string26 Yes! it is present in the string17 Yes! it is present in the string3 Yes! it is present in the string10 Yes! it is present in the string00 Yes! it is present in the string004 Yes! it is present in the string25 Yes! it is present in the string5 Yes! it is present in the string17 Yes! it is present in the string7 Yes! it is present in the string13 Yes! it is present in the string04 Yes! it is present in the string32 Yes! it is present in the string012 Đầu ra Yes! it is present in the string9 Phương pháp. Sử dụng hàm lambdaPython3Yes! it is present in the string13 Yes! it is present in the string3 Yes! it is present in the string26 Yes! it is present in the string17 Yes! it is present in the string3 Yes! it is present in the string10 Yes! it is present in the string019 Yes! it is present in the string3_______1021 Yes! it is present in the string01 Yes! it is present in the string023 Yes! it is present in the string01 Yes! it is present in the string025 Yes! it is present in the string026 Yes! it is present in the string7 Yes! it is present in the string028 |