Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
1 like 0 dislike
1.7k views
in Programming by 1 1 3
edited by

I am a newbie in Python, I am looking for the best way to check if the statement contains a specific string!

Ex: I want to check if this statement "Python is a programming language" contains a "Python" word or not!

Thanks for any inputs!


2 Answers

3 like 0 dislike
by 2 3 13
selected by
 
Best answer

In fact, there is no a frank contains function in Python. meanwhile,

There are two ways to check if string contains specific substring in Python:-

  1. Using “ in“ operator:

     str = "Python is a programming language"
     if "Python" in str
         # write your code here if it true
    
    
  2. Using” find” method that will return an index if it found the substring:

     Else it return -1
     find method
     str.find("tion")
    
by 1 1 3
0 0
It's a great answer, thanks for your help!
by 2 3 13
0 0
Glad to help!
1 like 0 dislike
by 1

Regular expression is widely used for pattern matching.

Python has a built-in package called re, which can be used to work with Regular Expressions .

The re module contains a function called search(), it can be used to check if a string contains the specified search pattern .

 re.search(pattern, string, flags[optional])

Ref: Contains in Paython

If you don’t ask, the answer is always NO!
...