• 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 / Check if String Does Not Contain Substring in Python

Check if String Does Not Contain Substring in Python

July 11, 2022 Leave a Comment

In Python, we can easily check if a string does not contains a substring using the in operator and not operator.

string = "Hello"

if "z" not in string:
    print("z not in string")
else:
    print("z in string")

#Output:
z not in string

When working with strings, it can be useful to know if a substring is contained in a string variable.

You can check if a string does not contain easily in Python.

To check if a string does not contain a particular substring, you can use the in operator and not operator.

Below is a simple example showing you how to check if a string does not contain another string in Python.

string = "Hello"

if "z" not in string:
    print("z not in string")
else:
    print("z in string")

#Output:
z not in string

Checking to See if a String Doesn’t Have Vowels in Python

You can check if a string doesn’t contain any vowels easily in Python.

To do so, you can use a loop and check if any vowel is contained in the string.

If none of the vowels are in the string, then you can conclude there are no vowels in the string.

Below is a simple example of how to check if a string has no vowels in Python.

string = "ccctttrrry"

def doesNotContainsVowels(s):
    string = string.lower()
    contains = False
    for char in string:
        if char in "aeiou":
           contains = True
    return contains

print(doesNotContainVowels("ccccttttwwx"))
print(doesNotContainVowels("a"))

#Output:
False
True

Hopefully this article has been useful for you to check if a string does not contain another string using Python.

Other Articles You'll Also Like:

  • 1.  How to Repeat a Function in Python
  • 2.  Touch File Python – How to Touch a File Using Python
  • 3.  e in Python – Using Math Module to Get Euler’s Constant e
  • 4.  Subtract Seconds from Datetime Variable Using Python timedelta() Function
  • 5.  pandas Correlation – Find Correlation of Series or DataFrame Columns
  • 6.  pandas startswith() – Check if String Starts With Certain Characters
  • 7.  Python Replace Space with Underscore Using String replace() Function
  • 8.  Shift Values in a List Using Python
  • 9.  Get Username in Python using os module
  • 10.  Using Python to Check If List of Words 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