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 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.
Python3
164
=
________ 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
if
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;
Python3
NO,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
8MyString1
9
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
MyString1
9
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
"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
if
0Yes! it is present in the string2
if
if
3if
4MyString1
8MyString1
9
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
MyString1
9
Yes! it is present in the string3
Yes! it is present in the string4
=
2Yes! it is present in the string6
"need"
9=
"A geek in need is a geek indeed"
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.
Python3
Yes! 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;
Python3
Is
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
8MyString1
9
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.
Python3
164
=
________ 26 & nbsp;MyString1
=
"A geek in need is a geek indeed"
if
"need"
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 []
MyString1
9
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.
MyString1
9
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 []
Python3
Yes! 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.
16
4=
________ 26 & nbsp;
Python3
Yes! 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
Python3
Yes! 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__