Python determines whether a string contains substrings

Source: Internet
Author: User

The Python string object does not have a contains method, and it is not possible to use the String.contains method to determine whether a substring is included, but Python has an easier way to replace the CONTAINS function

The Python string object does not have a contains method, and it is not possible to use the String.contains method to determine whether a substring is included, but Python has an easier way to replace the CONTAINS function

The Python string object does not have a contains method, and it is not possible to use the String.contains method to determine whether a substring is included, but Python has an easier way to replace the CONTAINS function

First, using the in method to achieve the function of contains:

1 ' http://www.baidu.com/ ' 2 if " Baidu " inch A: 3    Print ('A contains ABC')
View Code

Second, using the Find function to realize the function of contains

 1  s =  "  This is a string   2  if  s.find ("  is  Span style= "COLOR: #800000" ">" ) = = -1:  3  print   "  No ' is ' here!   " 4  else  :  5  print   " found ' is ' in the string.   " 
View Code

The above 2 ways can be!

Python determines whether a string contains substrings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.