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;

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ông

Có, 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:

  • Sử dụng IF IF IN & NBSP;
  • Kiểm tra bằng phương thức Split ()
  • Sử dụng phương thức Find ()
  • Sử dụng phương thức tính toán ()
  • Sử dụng phương thức index ()
  • Sử dụng & nbsp; __ chứa __ '& nbsp; lớp ma thuật.
  • Sử dụng biểu thức chính quy & nbsp;

Phương pháp 1: Kiểm tra chuỗi con bằng cách sử dụng if.

Python3

MyString1 = "A geek in need is a geek indeed"

if "need"

Yes! it is present in the string
0
Yes! it is present in the string
1

Yes! it is present in the string
2
Yes! it is present in the string
3
Yes! it is present in the string
4
Yes! it is present in the string
5
Yes! it is present in the string
6

Yes! it is present in the string
7
Yes! it is present in the string
8

Yes! it is present in the string
2
Yes! it is present in the string
3
Yes! it is present in the string
4
16
2
Yes! it is present in the string
6

Đầ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.

Python3

16
4= ________ 26 & nbsp;

16
7= ________ 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

if

16
7
Yes! it is present in the string
0
Yes! Geeks-13 is containing.
Yes! Geeks-78 is containing.
6

Yes! it is present in the string
2
Yes! it is present in the string
3
Yes! it is present in the string
4
NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
0
Yes! it is present in the string
6

Yes! it is present in the string
7
Yes! it is present in the string
8

Yes! it is present in the string
2
Yes! it is present in the string
3
Yes! it is present in the string
4
NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
7
Yes! it is present in the string
6

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;

Python3

NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
9 MyString1 0

Yes! it is present in the string
2if MyString1 3== MyString1 6MyString1 7MyString1 8

MyString1 9

Yes! it is present in the string
3
Yes! it is present in the string
4=2
Yes! it is present in the string
6

Yes! it is present in the string
2
Yes! it is present in the string
7
Yes! it is present in the string
8

MyString1 9

Yes! it is present in the string
3
Yes! it is present in the string
4"A geek in need is a geek indeed"0
Yes! it is present in the string
6

16
4=
16
6

"A geek in need is a geek indeed"5= "A geek in need is a geek indeed"7

"A geek in need is a geek indeed"8

Phương pháp 4: Kiểm tra Subring & NBSP; sử dụng phương thức đếm () Phương thức

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 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.

Python3

NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
9 if0

Yes! it is present in the string
2if if3if4MyString1 8

MyString1 9

Yes! it is present in the string
3
Yes! it is present in the string
4"A geek in need is a geek indeed"0
Yes! it is present in the string
6

Yes! it is present in the string
2
Yes! it is present in the string
7
Yes! it is present in the string
8

MyString1 9

Yes! it is present in the string
3
Yes! it is present in the string
4=2
Yes! it is present in the string
6

"need"9= "A geek in need is a geek indeed"

Yes! it is present in the string
02=
16
9

Yes! it is present in the string
05

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.

Python3

Yes! it is present in the string
06=
Yes! it is present in the string
08

Yes! it is present in the string
09= if4

Yes! it is present in the string
12=
Yes! it is present in the string
14

Yes! it is present in the string
3
Yes! it is present in the string
16
Yes! it is present in the string
17
Yes! it is present in the string
18

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;

Python3

Is

Yes! it is present in the string
30
Yes! it is present in the string
31
Yes! it is present in the string
0
Yes! it is present in the string
33

Yes! it is present in the string
2if
Yes! it is present in the string
36
Yes! it is present in the string
37MyString1 8

MyString1 9

Yes! it is present in the string
3
Yes! it is present in the string
41
Yes! it is present in the string
42
Yes! it is present in the string
6

Đầ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. 

Python3

16
4= ________ 26 & nbsp;

MyString1 = "A geek in need is a geek indeed"

if "need"

Yes! it is present in the string
0
Yes! it is present in the string
1

Đầu ra

Phương pháp 2: Kiểm tra chuỗi con bằng phương thức Split ()

MyString1 9

Yes! it is present in the string
62

Yes! it is present in the string
7
Yes! it is present in the string
8

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.

MyString1 9

Yes! it is present in the string
62

Đầ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 ()

Python3

Yes! it is present in the string
74=
16
6

Yes! it is present in the string
77=
16
9

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;

Python3

Yes! it is present in the string
74=
16
6

Yes! it is present in the string
77=
16
9

Các

Yes! it is present in the string
3
Yes! it is present in the string
81
NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
0 if
16
10__17

Phương pháp: Sử dụng chức năng Countof & nbsp;Using countof function 

Python3

Yes! it is present in the string
44
16
15

Yes! it is present in the string
74=
16
6

Yes! it is present in the string
777__6
16
9

Yes! it is present in the string
3
Yes! it is present in the string
81
NO,string 'geeks' is not present in string 'A geek in need is a geek indeed' 
0 if
16
26if4
Yes! it is present in the string
7
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 ().