• 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
  • VBA
  • 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.  pandas product – Get Product of Series or DataFrame Columns
  • 2.  Symmetric Difference of Two Sets in Python
  • 3.  Truncate String in Python with String Slicing
  • 4.  Euclidean Algorithm and Extended Euclidean Algorithm in Python
  • 5.  Remove Last Element from List Using Python
  • 6.  Length of Tuple Python – Find Tuple Length with Python len() Function
  • 7.  pandas set_value – Using at() Function to Set a Value in DataFrame
  • 8.  Python Even or Odd – Check if Number is Even or Odd Using % Operator
  • 9.  Convert String into Tuple in Python
  • 10.  Adjusting Python Turtle Screen Size with screensize() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy