InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python string contains check

    on Apr 11, 2016

    To check if a string is part of another string (case sensitive) we can use these approaches

    • Using in operator
    • Using string find() method
      ## if start and end are present then search from start till end-1
      s.find(sub[, start[, end]])
      
    • Using strng index() method. It is similar to find but raises ValueError. Its better to avoid it.
      ## if start and end are present then search from start till end-1
      s.index(sub[, start[, end]])
      

    Case insensitive check

    To do case insensitive check, both string can be converted to lowercase.

    Example – contains check using in operator

    s = "Hello Python"
    sub = "Hello"
    if sub in s:
      print "found"
    found
    
    Env: Python 2.7.18

    Example – contains check using find(sub)

    s = "Hello Python"
    sub = "Hello"
    if s.find(sub) >= 0:
      print "found at %s" % (s.find(sub))
    found at 0
    
    Env: Python 2.7.18

    Example – contains check using index(sub)

    Since this raises ValueError, it is better to use above approaches.

    s = "Hello Python"
    sub = "Hello2"
    try:
      if s.index(sub) >= 0:
        print "found at %s" % (s.index(sub))
    except ValueError as e:
      print str(e)
    substring not found
    
    Env: Python 2.7.18

    Suggested posts:

    1. Python int to string
    2. Python string split examples
    3. Python for loop examples
    4. Python find length of string or list using len()
    5. Python if-else and if-elif-else
    6. CSS max-height – limit maximum height of an element
    7. Python dictionary basics
    8. Python file read write examples
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress