We often encounter such a need:Determine whether a string contains a keyword, i.e., a specific substring
For example, finding the title of a book that contains "python" in a pile of book titles. For example, find a book title that contains "python" from a pile of book titles.
Determining that two strings are equal is simple and straightforward== That's it. It's actually pretty easy to determine the contained substring, and there's more than one way to do it. We'll share with you the7 A method that can achieve this effect:
1. Use in and not in
in cap (a poem)not in existPython in are very common keywords, and we categorize them as membership operators.
Using these two membership operators allows us to determine whether an object is in another object in a very intuitive and clear way, as shown in the following example:
>>> "llo" in "hello, python" True >>> >>> "lol" in "hello, python" False
2、Use the find method
Use the string object'sfind method, which returns the occurrence of the specified substring in the string if the substring was found, or the location of the substring if it was not found.-1
>>> "hello, python".find("llo") != -1 True >>> "hello, python".find("lol") != -1 False >>
3. Use the index method
String objects have aindex method, which returns the index of the first occurrence of the specified substring in the string, and throws an exception if it is not found, so care needs to be taken to catch it when using it.
def is_in(full_str, sub_str): try: full_str.index(sub_str) return True except ValueError: return False print(is_in("hello, python", "llo")) # True print(is_in("hello, python", "lol")) # False
4. Use the count method
utilization andindex This same kind of curvilinear thinking is available to us in the form of acount The method of judgment.
As long as the judgment result is greater than0 It means that the substring exists in the string.
def is_in(full_str, sub_str): return full_str.count(sub_str) > 0 print(is_in("hello, python", "llo")) # True print(is_in("hello, python", "lol")) # False
5. Through magical methods
In the first approach, we usein cap (a poem)not in determines whether a substring exists in another character, which is actually the case when you use thein cap (a poem)not in whenPython The interpreter will first go and check if the object has a__contains__
Magic Methods.
If it does, execute it. If it doesn't.Python It automatically iterates through the entire sequence, returning as soon as it finds the desired item.True 。
Examples are shown below;
>>> "hello, python".__contains__("llo") True >>> >>> "hello, python".__contains__("lol") False >>>
This usage is similar to the use ofin respond in singingnot in There's no difference, but it doesn't rule out the possibility that someone would deliberately write it that way to make the code more difficult to understand.
6, with the help of operator
operatormodules arepython in the built-in operator function interface, which defines functions for some of the arithmetic and comparison built-in operations.operatorModules are created using thec implementation, so the execution is faster thanpython Code fast.
existoperator There is a method in thecontains It is easy to determine whether a substring is in a string.
>>> import operator >>> >>> ("hello, python", "llo") True >>> ("hello, python", "lol") False >>>
7, the use of regular matching
When it comes to the find function, that regular can definitely be said to be a professional tool, how complex find rules, can meet you.
For this requirement of determining whether a string exists in another string, using a regular is simply too big a deal.
import re def is_in(full_str, sub_str): if (sub_str, full_str): return True else: return False print(is_in("hello, python", "llo")) # True print(is_in("hello, python", "lol")) # False
to this article on Python to determine whether the string contains a specific substring of the seven methods of the article is introduced to this, more related to Python to determine whether the string contains a specific substring of content, please search for my previous posts or continue to browse the following articles I hope that you will support me in the future more!