Kiểm tra xem từ không có trong chuỗi python

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.  

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

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

  • Sử dụng if… trong
  • 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 “count()”
  • Sử dụng phương thức index()
  • Sử dụng __contains__” lớp ma thuật
  • Sử dụng biểu thức chính quy

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

Python3




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
9
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
02
Yes! it is present in the string
03

Yes! it is present in the string
04
Yes! it is present in the string
05

Yes! it is present in the string
9
Yes! it is present in the string
00____101
Yes! it is present in the string
09
Yes! it is present in the string
03

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

Python3




Yes! it is present in the string
21

Yes! it is present in the string
22

 

Yes! it is present in the string
23

Yes! it is present in the string
24
Yes! it is present in the string
3
Yes! it is present in the string
26 
Yes! it is present in the string
27

Yes! it is present in the string
28
Yes! it is present in the string
3
Yes! it is present in the string
10 
Yes! it is present in the string
11

 

Yes! it is present in the string
12

Yes! it is present in the string
13
Yes! it is present in the string
3
Yes! it is present in the string
15

 

Yes! it is present in the string
16

Yes! it is present in the string
17

Yes! it is present in the string
5
Yes! it is present in the string
28
Yes! it is present in the string
7
Yes! it is present in the string
21

Yes! it is present in the string
9
Yes! it is present in the string
00____101
Yes! it is present in the string
25
Yes! it is present in the string
03

Yes! it is present in the string
04
Yes! it is present in the string
05

Yes! it is present in the string
9
Yes! it is present in the string
00____101
Yes! it is present in the string
32
Yes! it is present in the string
03

Đầu ra

Yes! it is present in the string
0

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.  

Python3




Yes! it is present in the string
34

Yes! it is present in the string
35

 

 

Yes! it is present in the string
36
Yes! it is present in the string
37

Yes! it is present in the string
9
Yes! it is present in the string
5
Yes! it is present in the string
20
Yes! it is present in the string
3
Yes! it is present in the string
3
Yes! it is present in the string
23
Yes! it is present in the string
24
Yes! it is present in the string
25

Yes! it is present in the string
26
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
29
Yes! it is present in the string
03

Yes! it is present in the string
9
Yes! it is present in the string
04
Yes! it is present in the string
05

Yes! it is present in the string
26
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
97
Yes! it is present in the string
03

 

 

Yes! it is present in the string
99

Yes! it is present in the string
24
Yes! it is present in the string
3
Yes! it is present in the string
26

Yes! it is present in the string
93____13
Yes! it is present in the string
95

Yes! it is present in the string
96

Đầu ra

Yes! it is present in the string
2

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”

Python3




Yes! it is present in the string
36
Yes! it is present in the string
98

Yes! it is present in the string
9
Yes! it is present in the string
5
Yes! it is present in the string
01
Yes! it is present in the string
02
Yes! it is present in the string
25

Yes! it is present in the string
26
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
97
Yes! it is present in the string
03

Yes! it is present in the string
9
Yes! it is present in the string
04
Yes! it is present in the string
05

Yes! it is present in the string
26
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
29
Yes! it is present in the string
03

 

 

Yes! it is present in the string
17
Yes! it is present in the string
3
Yes! it is present in the string
4

Yes! it is present in the string
20
Yes! it is present in the string
3
Yes! it is present in the string
10

Yes! it is present in the string
23

Đầu ra

Yes! it is present in the string
1

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

Python3




Yes! it is present in the string
24
Yes! it is present in the string
3
Yes! it is present in the string
26

Yes! it is present in the string
27
Yes! it is present in the string
3
Yes! it is present in the string
02

Yes! it is present in the string
30
Yes! it is present in the string
3
Yes! it is present in the string
32

Yes! it is present in the string
00
Yes! it is present in the string
34
Yes! it is present in the string
35
Yes! it is present in the string
36

đầu ra

Yes! it is present in the string
2

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.  

Python3




Yes! it is present in the string
37
Yes! it is present in the string
3
Yes! it is present in the string
39_______140
Yes! it is present in the string
41
Yes! it is present in the string
42
Yes! it is present in the string
41
Yes! it is present in the string
44
Yes! it is present in the string
41
Yes! it is present in the string
46
Yes! it is present in the string
47

Yes! it is present in the string
48
Yes! it is present in the string
49
Yes! it is present in the string
7
Yes! it is present in the string
51

Yes! it is present in the string
9
Yes! it is present in the string
5
Yes! it is present in the string
54
Yes! it is present in the string
55
Yes! it is present in the string
25

Yes! it is present in the string
26
Yes! it is present in the string
00____159
Yes! it is present in the string
60
Yes! it is present in the string
03

Đầu ra

Yes! it is present in the string
3

Phương pháp 7. Kiểm tra chuỗi con bằng biểu thức chính quy

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

Python3




Yes! it is present in the string
62

Yes! it is present in the string
63

Yes! it is present in the string
64
Yes! it is present in the string
65

 

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
70
Yes! it is present in the string
3
Yes! it is present in the string
10

 

Yes! it is present in the string
73

Yes! it is present in the string
74

Yes! it is present in the string
5
Yes! it is present in the string
76

Yes! it is present in the string
9
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
80
Yes! it is present in the string
81
Yes! it is present in the string
82
Yes! it is present in the string
01

Yes! it is present in the string
26
Yes! it is present in the string
85

Yes! it is present in the string
04
Yes! it is present in the string
05

Yes! it is present in the string
9
Yes! it is present in the string
00
Yes! it is present in the string
01
Yes! it is present in the string
91
Yes! it is present in the string
81
Yes! it is present in the string
82
Yes! it is present in the string
01

Yes! it is present in the string
26
Yes! it is present in the string
85

Đầu ra

Yes! it is present in the string
2

Phương pháp. Sử dụng hiểu danh sách

Python3




Yes! it is present in the string
13
Yes! it is present in the string
3
Yes! it is present in the string
26

Yes! it is present in the string
17
Yes! it is present in the string
3
Yes! it is present in the string
10

Yes! it is present in the string
00
Yes! it is present in the string
004
Yes! it is present in the string
25
Yes! it is present in the string
5
Yes! it is present in the string
17
Yes! it is present in the string
7
Yes! it is present in the string
13
Yes! it is present in the string
04
Yes! it is present in the string
32
Yes! it is present in the string
012

Đầu ra

Yes! it is present in the string
9

Phương pháp. Sử dụng hàm lambda

Python3




Yes! it is present in the string
13
Yes! it is present in the string
3
Yes! it is present in the string
26

Yes! it is present in the string
17
Yes! it is present in the string
3
Yes! it is present in the string
10

Yes! it is present in the string
019
Yes! it is present in the string
3_______1021
Yes! it is present in the string
01
Yes! it is present in the string
023
Yes! it is present in the string
01
Yes! it is present in the string
025
Yes! it is present in the string
026
Yes! it is present in the string
7
Yes! it is present in the string
028