Hướng dẫn how to check substring in python - cách kiểm tra chuỗi con trong python
Trong bài viết này, chúng tôi sẽ đề cập đến cách kiểm tra xem chuỗi Python có chứa một chuỗi khác hoặc một chuỗi con trong Python không. Cho hai chuỗi, hãy kiểm tra xem một chuỗi con có có trong chuỗi đã cho hay không. & NBSP; 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ó một chuỗi chứa phương thức chuỗi con khôngCó, kiểm tra một chuỗi con là một trong những nhiệm 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 có chứa chuỗi con như, find (), index (), Count (), v.v. . Ở đây chúng tôi sẽ bao gồm các cách tiếp cận khác nhau như:in” operator which is used as a comparison operator. Here we will cover different approaches like:
Phương pháp 1: Kiểm tra chuỗi con bằng cách sử dụng if.Python3
Yes! it is present in the string0 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 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 string2 Yes! it is present in the string3 Yes! it is present in the string4 162 Yes! it is present in the string6 Đầ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 một chuỗi con có mặt trong chuỗi đã cho hay không mà không sử dụng bất kỳ chức năng 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 một chuỗi con có có trong chuỗi đã cho hay không. Python3164 = ________ 26 & nbsp;167 = ________ 29 & nbsp;Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.0____6 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.2
167 Yes! it is present in the string0 Yes! Geeks-13 is containing. Yes! Geeks-78 is containing.6 Yes! it is present in the string2 Yes! it is present in the string3 Yes! it is present in the string4 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'0 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 string2 Yes! it is present in the string3 Yes! it is present in the string4 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7 Yes! it is present in the string6 Phương pháp 3: Kiểm tra chuỗi con & nbsp; sử dụng phương thức find ()Chúng tôi có thể kiểm tra một cách lặp đi lặp lại cho mỗi từ, nhưng Python cung cấp cho chúng tôi một hàm sẵn () kiểm tra xem có thể có trong chuỗi trong chuỗi, được thực hiện trong một dòng. find () hàm trả về -1 Nếu không tìm thấy, nếu không nó sẽ trả về lần xuất hiện đầu tiên, do đó, sử dụng chức năng này, vấn đề này có thể được giải quyết. & nbsp; Python3NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'9 MyString1 0Yes! it is present in the string2 if MyString1 3= = MyString1 6MyString1 7MyString1 8
Yes! it is present in the string3 Yes! it is present in the string4 = 2Yes! it is present in the string6 Yes! it is present in the string2 Yes! it is present in the string7 Yes! it is present in the string8
Yes! it is present in the string3 Yes! it is present in the string4 "A geek in need is a geek indeed" 0Yes! it is present in the string6 164 = 166
Phương pháp 4: Kiểm tra Subring & NBSP; sử dụng phương thức đếm () Phương thứcBạ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 Python Count (). Nếu không tìm thấy chất nền thì không có, thì có, sẽ không in nếu không sẽ không được in. Python3NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'9 if 0Yes! it is present in the string2 if if 3if 4MyString1 8
Yes! it is present in the string3 Yes! it is present in the string4 "A geek in need is a geek indeed" 0Yes! it is present in the string6 Yes! it is present in the string2 Yes! it is present in the string7 Yes! it is present in the string8
Yes! it is present in the string3 Yes! it is present in the string4 = 2Yes! it is present in the string6
Yes! it is present in the string02 = 169 Yes! it is present in the string05 Phương pháp 5: Kiểm tra chuỗi con bằng phương thức Index ()Phương thức .index () Trả về chỉ số bắt đầu của chuỗi con được truyền dưới dạng tham số. Ở đây, Sub Substring có mặt tại INDEX 16..index() method returns the starting index of the substring passed as a parameter. Here “substring” is present at index 16. Python3Yes! it is present in the string06 = Yes! it is present in the string08 Yes! it is present in the string09 = if 4Yes! it is present in the string12 = Yes! it is present in the string14 Yes! it is present in the string3 Yes! it is present in the string16 Yes! it is present in the string17 Yes! it is present in the string18 Output: 16 Phương pháp 6: Kiểm tra chuỗi con & nbsp; sử dụng lớp __contains __, & nbsp; lớp ma thuật.Chuỗi Python __Contains __ (). Phương thức này được sử dụng để kiểm tra xem chuỗi có mặt trong chuỗi khác hay không. & NBSP; Python3Is Yes! it is present in the string30 Yes! it is present in the string31 Yes! it is present in the string0 Yes! it is present in the string33 Yes! it is present in the string2 if Yes! it is present in the string36 Yes! it is present in the string37 MyString1 8
Yes! it is present in the string3 Yes! it is present in the string41 Yes! it is present in the string42 Yes! it is present in the string6 Đầu ra Yes! Geeks-13 is containing. Yes! Geeks-78 is containing. Phương pháp 2: Kiểm tra chuỗi con bằng phương thức Split ()Kiểm tra xem một chuỗi con có mặt trong chuỗi đã cho hay không mà không sử dụng bất kỳ chức năng 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 một chuỗi con có có trong chuỗi đã cho hay không.re, which can be used to work with Regular Expressions. Python3164 = ________ 26 & nbsp;
Yes! it is present in the string0 Yes! it is present in the string1 Đầu ra Phương pháp 2: Kiểm tra chuỗi con bằng phương thức Split ()
Yes! it is present in the string62 Yes! it is present in the string7 Yes! it is present in the string8 Kiểm tra xem một chuỗi con có mặt trong chuỗi đã cho hay không mà không sử dụng bất kỳ chức năng 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 một chuỗi con có có trong chuỗi đã cho hay không.
Yes! it is present in the string62 Đầu ra NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' Phương pháp 2: Kiểm tra chuỗi con bằng phương thức Split ()Python3Yes! it is present in the string74 = 166 Yes! it is present in the string77 = 169 Kiểm tra xem một chuỗi con có mặt trong chuỗi đã cho hay không mà không sử dụng bất kỳ chức năng 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 một chuỗi con có có trong chuỗi đã cho hay không. 164= ________ 26 & nbsp;Python3Yes! it is present in the string74 = 166 Yes! it is present in the string77 = 169 Các Yes! it is present in the string3 Yes! it is present in the string81 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'0 if 1610__17 Phương pháp: Sử dụng chức năng Countof & nbsp;Using countof function Python3Yes! it is present in the string44 1615 Yes! it is present in the string74 = 166 Yes! it is present in the string777__6 169 Yes! it is present in the string3 Yes! it is present in the string81 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'0 if 1626 if 4 Yes! it is present in the string7 NO,string 'geeks' is not present in string 'A geek in need is a geek indeed'7__ Làm cách nào để kiểm tra xem một bộ nền có ở Python không?Người vận hành trong nó trả về một boolean (đúng hoặc sai). Để kiểm tra xem một chuỗi có chứa chuỗi con trong Python bằng toán tử trong không, chúng tôi chỉ cần gọi nó trên SuperString: FullString = "Stackabuse" Subring = "Tack" nếu Substring in FullString: Print ("Tìm thấy!") Không tìm thấy!")
It returns a Boolean (either True or False ). To check if a string contains a substring in Python using the in operator, we simply invoke it on the superstring: fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!")
Làm thế nào để bạn kiểm tra một chuỗi con?Bạn có thể sử dụng phương thức chứa (), indexof () và lastindexof () để kiểm tra xem một chuỗi có chứa một chuỗi khác trong java hay không.Nếu một chuỗi chứa một chuỗi khác thì nó được gọi là chuỗi con.Phương thức indexof () chấp nhận một chuỗi và trả về vị trí bắt đầu của chuỗi nếu nó tồn tại, nếu không, nó sẽ trả về -1.use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.
Làm thế nào để tôi tìm thấy một chuỗi con trong một danh sách các chuỗi trong Python?Chúng ta hãy thảo luận về một số cách để tìm các chuỗi có chuỗi con đã cho trong danh sách ... Phương pháp số 1: Sử dụng danh sách hiểu.Danh sách hiểu là một cách thanh lịch để thực hiện bất kỳ nhiệm vụ cụ thể nào vì nó tăng khả năng đọc trong một thời gian dài..... Phương pháp số 2: Sử dụng Filter () + Lambda..... Phương pháp số 3: Sử dụng re + search (). |