Hướng dẫn how do i check if a substring is in python? - làm cách nào để kiểm tra xem chuỗi con có trong python không?

Trong hướng dẫn này, chúng ta sẽ xem cách kiểm tra xem một chuỗi có chứa một chuỗi con trong Python không. Như thường lệ, mỗi cách tiếp cận chúng tôi sẽ đề cập có những ưu và nhược điểm khác nhau.

Các nhà điều hành trong

Cách dễ nhất để kiểm tra xem chuỗi Python có chứa chất nền là sử dụng toán tử

TypeError: argument of type 'NoneType' is not iterable
1 không.

Toán tử

TypeError: argument of type 'NoneType' is not iterable
1 được sử dụng để kiểm tra các cấu trúc dữ liệu để thành viên trong Python. Nó trả về một boolean (hoặc
TypeError: argument of type 'NoneType' is not iterable
3 hoặc
TypeError: argument of type 'NoneType' is not iterable
4). Để kiểm tra xem một chuỗi có chứa chuỗi con trong Python bằng toán tử
TypeError: argument of type 'NoneType' is not iterable
1 hay không, chúng tôi chỉ cần gọi nó trên SuperString:

fullstring = "StackAbuse"
substring = "tack"

if substring in fullstring:
    print("Found!")
else:
    print("Not found!")

Toán tử này là tốc ký để gọi phương thức

TypeError: argument of type 'NoneType' is not iterable
6 của đối tượng và cũng hoạt động tốt để kiểm tra xem một mục có tồn tại trong danh sách không. Điều đáng chú ý là nó không an toàn, vì vậy nếu
TypeError: argument of type 'NoneType' is not iterable
7 của chúng tôi chỉ ra
TypeError: argument of type 'NoneType' is not iterable
8, một ngoại lệ sẽ bị ném:

TypeError: argument of type 'NoneType' is not iterable

Để tránh điều này, trước tiên bạn sẽ muốn kiểm tra xem nó có chỉ vào

TypeError: argument of type 'NoneType' is not iterable
8 hay không:

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")

Phương thức chuỗi.index ()

Loại chuỗi trong Python có một phương thức gọi là

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
0 có thể được sử dụng để tìm chỉ số bắt đầu của lần xuất hiện đầu tiên của chuỗi con trong chuỗi.

Kiểm tra hướng dẫn thực hành của chúng tôi, thực tế để học Git, với các thực hành tốt nhất, các tiêu chuẩn được công nghiệp chấp nhận và bao gồm bảng gian lận. Ngừng các lệnh git googling và thực sự tìm hiểu nó!

Nếu không tìm thấy chất nền, ngoại lệ

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
1 bị ném, có thể được xử lý với khối Expet-Offcept-Else:

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")

Phương pháp này rất hữu ích nếu bạn cần biết vị trí của chuỗi con, trái ngược với sự tồn tại của nó trong toàn bộ chuỗi.

Phương thức chuỗi.find ()

Loại chuỗi có một phương pháp khác được gọi là

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
2 thuận tiện hơn để sử dụng so với
fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
0, vì chúng tôi không cần phải lo lắng về việc xử lý bất kỳ ngoại lệ nào.

Nếu

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
4 không tìm thấy một trận đấu, nó sẽ trả về -1, nếu không, nó sẽ trả về chỉ số bên trái nhất của chuỗi con trong chuỗi lớn hơn.

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")

Nếu bạn muốn tránh cần phải bắt lỗi, thì phương pháp này sẽ được ưu tiên hơn

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
0.

Biểu thức thông thường (Regex)

Biểu thức chính quy cung cấp một cách linh hoạt hơn (mặc dù phức tạp hơn) để kiểm tra các chuỗi để khớp mẫu. Python được vận chuyển với một mô-đun tích hợp cho các biểu thức thông thường, được gọi là

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
6. Mô -đun
fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
6 chứa một hàm gọi là
fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
8, chúng ta có thể sử dụng để phù hợp với mẫu con:

from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"

Phương pháp này là tốt nhất nếu bạn cần một chức năng phù hợp phức tạp hơn, như phù hợp với trường hợp không nhạy cảm. Nếu không, nên tránh tốc độ biến chứng và tốc độ chậm hơn cho các trường hợp sử dụng phù hợp với chất nền đơn giản.

Bài viết này được viết bởi Jacob Stopak, một nhà tư vấn và nhà phát triển phần mềm với niềm đam mê giúp đỡ người khác cải thiện cuộc sống của họ thông qua mã. Jacob là người tạo ra cam kết ban đầu - một trang web dành riêng để giúp các nhà phát triển tò mò tìm hiểu cách các chương trình yêu thích của họ được mã hóa. Dự án nổi bật của nó giúp mọi người học Git ở cấp độ mã.

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

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
9
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
1

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
2
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
3
TypeError: argument of type 'NoneType' is not iterable
1
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
5

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
9
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
1
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
2

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
6
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

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

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
8
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0 ________ 50 & nbsp;

from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
1
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0 ________ 53 & nbsp;

from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
4
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
6

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
2
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
1
TypeError: argument of type 'NoneType' is not iterable
1
Example 1: Input : Substring = "geeks" 
           String="geeks for geeks"
Output : yes
Example 2: Input : Substring = "geek"
           String="geeks for geeks"
Output : yes
0

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
Example 1: Input : Substring = "geeks" 
           String="geeks for geeks"
Output : yes
Example 2: Input : Substring = "geek"
           String="geeks for geeks"
Output : yes
4
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
1
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
2

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
Yes! it is present in the string
1
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

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

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

Các

16
3
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
16
6
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
1
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
2

16
3
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
Yes! Geeks-13 is containing.
Yes! Geeks-78 is containing.
4
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
8
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
0

Yes! Geeks-13 is containing.
Yes! Geeks-78 is containing.
9
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
TypeError: argument of type 'NoneType' is not iterable
01

TypeError: argument of type 'NoneType' is not iterable
02

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

Yes! it is present in the string
3
TypeError: argument of type 'NoneType' is not iterable
04

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
2
TypeError: argument of type 'NoneType' is not iterable
07__108
16
2

16
3
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
Yes! Geeks-13 is containing.
Yes! Geeks-78 is containing.
4
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
1
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
2

16
3
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
8
16
6
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

TypeError: argument of type 'NoneType' is not iterable
23
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
1

TypeError: argument of type 'NoneType' is not iterable
26
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
3

TypeError: argument of type 'NoneType' is not iterable
29

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

TypeError: argument of type 'NoneType' is not iterable
30
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
TypeError: argument of type 'NoneType' is not iterable
32

TypeError: argument of type 'NoneType' is not iterable
33
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
TypeError: argument of type 'NoneType' is not iterable
08

TypeError: argument of type 'NoneType' is not iterable
36
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
TypeError: argument of type 'NoneType' is not iterable
38

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
TypeError: argument of type 'NoneType' is not iterable
40
TypeError: argument of type 'NoneType' is not iterable
41
TypeError: argument of type 'NoneType' is not iterable
42

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

TypeError: argument of type 'NoneType' is not iterable
43
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
TypeError: argument of type 'NoneType' is not iterable
45
TypeError: argument of type 'NoneType' is not iterable
46
TypeError: argument of type 'NoneType' is not iterable
47
TypeError: argument of type 'NoneType' is not iterable
48
TypeError: argument of type 'NoneType' is not iterable
47
TypeError: argument of type 'NoneType' is not iterable
50
TypeError: argument of type 'NoneType' is not iterable
47
TypeError: argument of type 'NoneType' is not iterable
522

TypeError: argument of type 'NoneType' is not iterable
54
TypeError: argument of type 'NoneType' is not iterable
55
TypeError: argument of type 'NoneType' is not iterable
1
TypeError: argument of type 'NoneType' is not iterable
57

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
6
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
2
TypeError: argument of type 'NoneType' is not iterable
60
TypeError: argument of type 'NoneType' is not iterable
61
16
2

16
3
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
7
TypeError: argument of type 'NoneType' is not iterable
65
TypeError: argument of type 'NoneType' is not iterable
66
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
0

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

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
8
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0 ________ 50 & nbsp;

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
9
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
1

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
2
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
3
TypeError: argument of type 'NoneType' is not iterable
1
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
5

Đầu ra

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

16
3
TypeError: argument of type 'NoneType' is not iterable
86

fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
1
fullstring = "StackAbuse"
substring = "tack"

if fullstring.find(substring) != -1:
    print("Found!")
else:
    print("Not found!")
2

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.

16
3
TypeError: argument of type 'NoneType' is not iterable
86

Đầu ra

TypeError: argument of type 'NoneType' is not iterable
0

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

Python3

TypeError: argument of type 'NoneType' is not iterable
98
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
0

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
01
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
3

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.

fullstring = "StackAbuse" substring = "tack" if fullstring.find(substring) != -1: print("Found!") else: print("Not found!") 8fullstring = "StackAbuse" substring = "tack" try: fullstring.index(substring) except ValueError: print("Not found!") else: print("Found!") 0 ________ 50 & nbsp;

Python3

TypeError: argument of type 'NoneType' is not iterable
98
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
0

fullstring = None
substring = "tack"

if fullstring != None and substring in fullstring:
    print("Found!")
else:
    print("Not found!")
01
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
3

from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
1
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0 ________ 53 & nbsp;

from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
4
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
6

fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
2
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
1
TypeError: argument of type 'NoneType' is not iterable
1
Example 1: Input : Substring = "geeks" 
           String="geeks for geeks"
Output : yes
Example 2: Input : Substring = "geek"
           String="geeks for geeks"
Output : yes
0
Using countof function 

Python3

Phương pháp 3: Kiểm tra chuỗi con & nbsp; sử dụng phương thức find ()

TypeError: argument of type 'NoneType' is not iterable
98
fullstring = "StackAbuse"
substring = "tack"

try:
    fullstring.index(substring)
except ValueError:
    print("Not found!")
else:
    print("Found!")
0
from re import search

fullstring = "StackAbuse"
substring = "tack"

if search(substring, fullstring):
    print "Found!"
else:
    print "Not found!"
0

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;

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