• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / String Contains Case Insensitive in Python

String Contains Case Insensitive in Python

July 26, 2022 Leave a Comment

To check if a string contains a substring and ignore the case of the characters in the string, you can use the Python in operator and the lower() function.

s = "this IS a StrING"

def containsCaseInsensitive(substring, string):
    if substring.lower() in string.lower():
        return True
    else:
        return False

print(containsCaseInsensitive("is",s))
print(containsCaseInsensitive("THIS",s))
print(containsCaseInsensitive("z",s))
 
#Output:
True
True
False

You can also use the Python upper() function if you want.

s = "this IS a StrING"

def containsCaseInsensitive(substring, string):
    if substring.upper() in string.upper():
        return True
    else:
        return False

print(containsCaseInsensitive("is",s))
print(containsCaseInsensitive("THIS",s))
print(containsCaseInsensitive("z",s))
 
#Output:
True
True
False

When processing string variables, the ability to check certain conditions is valuable.

One such case is if you want to perform a case-insensitive search and see if a string is contained in another string if we ignore case.

In Python, we can create a contains case insensitive function easily with the Python in operator and the lower() function.

in in Python allows us to see if a string is contained in a string, but is case sensitive.

If you want to check if a string is contained in another and ignore case, we need to use lower() to convert both strings to lowercase.

Then you can see if the lowercase string is contained in the other lowercase string.

Below is an example showing you how to see if a string is contained in another string ignoring case in Python.

s = "this IS a StrING"

def containsCaseInsensitive(substring, string):
    if substring.lower() in string.lower():
        return True
    else:
        return False

print(containsCaseInsensitive("is",s))
print(containsCaseInsensitive("THIS",s))
print(containsCaseInsensitive("z",s))
 
#Output:
True
True
False

Hopefully this article has been useful for you to check if a string contains another string case insensitive in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Check if Deque is Empty
  • 2.  PROC PHREG Equivalent in Python
  • 3.  Draw Star in Python Using turtle Module
  • 4.  Python nth Root – Find nth Root of Number with math.pow() Function
  • 5.  Count Spaces in String in Python
  • 6.  How to Combine Dictionaries in Python
  • 7.  How to Check if Tuple is Empty in Python
  • 8.  How to Output XLSX File from Pandas to Remote Server Using Paramiko FTP
  • 9.  Truncate String in Python with String Slicing
  • 10.  Using Python to Insert Tab in String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy