I will introduce a new topic to you today: Check if a string does NOT contain a substring in Python. This is a fundamental topic; to do this, I will use the functions available in Python such as str.index(), str.find()…Details are in the article below.
Check if a string does NOT contain substring in Python.
Use the Authentication operator
Example:
originalStr = 'Hello! Visit LearnshareIt to read great articles on programming languages.' subStr = 'LearnshareIt' # Use the Authentication operator. if subStr not in originalStr: print('The substring does not appear in the original string.') else: print('The substring appears in the original string.')
Output:
The substring appears in the original string.
Use the str.index() function
Syntax:
str.index(str, start, end)
Parameters:
- str: Specifies the search string.
- start: The starting index. The default is 0.
- end: End index. The default is the string length.
Example:
originalStr = 'Hello! Visit LearnshareIt to read great articles on programming languages.' subStr = 'LearnshareIt' try: # Use the str.index() to check for occurrence of substring. originalStr.index(subStr) != -1 print('The substring appears in the original string.') except ValueError: print('The substring does not appear in the original string.')
Output:
The substring appears in the original string.
Note: The str.index() function will return an exception if the substring is not found.
Use the str.find() method
Like the str.index() function, you can use the str.find() function with the same function and parameters.
Syntax:
str.find(str, start, end)
Parameters:
- str: Specifies the search string.
- start: The starting index. The default is 0.
- end: End index. The default is the string length.
Example:
originalStr = 'Hello! Visit LearnshareIt to read great articles on programming languages.' subStr = 'LearnshareIt' # Use the str.find() to check for occurrence of substring. if originalStr.find(subStr) != -1: print('The substring appears in the original string.') else: print('The substring does not appear in the original string.')
Output:
The substring appears in the original string.
Use the re.search() function
Syntax:
re.search(pattern, str)
Parameters:
- pattern: is RegEx.
- str: search string.
Example:
import re originalStr = 'Hello! Visit LearnshareIt to read great articles on programming languages.' subStr = 'LearnshareIt' # Use the re.search() to check for occurrence of substring. if re.search(subStr, originalStr): print('The substring appears in the original string.') else: print('The substring does not appear in the original string.')
Output:
The substring appears in the original string.
Summary
The article Check if a string does NOT contain substring in Python is probably solved. You can use one of the four methods above because they are entirely built-in functions in Python. If you have any questions or have a more creative way, I hope you will share it with everyone by commenting below. Thank you for reading my article.

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java