Python - Check if substring present in string Last Updated : 05 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The task is to check if a specific substring is present within a larger string. Python offers several methods to perform this check, from simple string methods to more advanced techniques. In this article, we'll explore these different methods to efficiently perform this check.Using in operatorThis operator is the fastest method to check for a substring, the power of in operator in Python is very well known and is used in many operations across the entire language. Python s= "GeeksforGeeks" # Check if "for" exists in `s` if "for" in s: print(True) else: print(False) OutputTrue Let's understand different methods to check if substring present in string.Table of ContentUsing str.find()Using str.index()Using re.search()Using str.find()find() method searches for a substring in a string and returns its starting index if found, or -1 if not found. It's useful for checking the presence of a specific word or phrase in a string. Python s= "GeeksforGeeks" # to check for substring res = s.find("for") if res >= 0: print(True) else: print(False) OutputTrue Explanation:s.find():This looks "for" word in `s` and gives its position. If not found, it returns -1.Using str.index()str.index() method helps us to find the position of a specific word or character in a string. If the word isn't found, it throws an error, unlike find() which just returns -1. It's useful when we want to catch the error if the word is missing. Python s= "GeeksforGeeks" try: # to check for substring res = s.index("for") print(True) except ValueError: print(False) OutputTrue Explanations.index("for"): This searches for the substring "for" in `s`.except ValueError: This catches the error if the substring is not found, and prints False.Using re.search()re.search() finds a pattern in a string using regular expressions. It's slower for simple searches due to extra processing overhead. Python import re s= "GeeksforGeeks" if re.search("for", s): print(True) else: print(False) OutputTrue Explanation:if re.search("for", s): This checks if the substring was found. If found, it returns a match object, which evaluates to True. Comment More infoAdvertise with us Next Article Python - Check if substring present in string manjeet_04 Follow Improve Article Tags : Python python-string Python string-programs Practice Tags : python Similar Reads Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx 8 min read How to Substring a String in Python A String is a collection of characters arranged in a particular order. A portion of a string is known as a substring. For instance, suppose we have the string "GeeksForGeeks". In that case, some of its substrings are "Geeks", "For", "eeks", and so on. This article will discuss how to substring a str 4 min read Check if a given string is binary string or not - Python The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi 3 min read String Subsequence and Substring in Python Subsequence and Substring both are parts of the given String with some differences between them. Both of them are made using the characters in the given String only. The difference between them is that the Substring is the contiguous part of the string and the Subsequence is the non-contiguous part 5 min read Python | Filter String with substring at specific position Sometimes, while working with Python string lists, we can have a problem in which we need to extract only those lists that have a specific substring at a specific position. This kind of problem can come in data processing and web development domains. Let us discuss certain ways in which this task ca 7 min read Python | Remove Redundant Substrings from Strings List Given list of Strings, task is to remove all the strings, which are substrings of other Strings. Input : test_list = ["Gfg", "Gfg is best", "Geeks", "for", "Gfg is for Geeks"] Output : ['Gfg is best', 'Gfg is for Geeks'] Explanation : "Gfg", "for" and "Geeks" are present as substrings in other strin 5 min read Python - Check if String Contain Only Defined Characters using Regex In this article, we are going to see how to check whether the given string contains only a certain set of characters in Python. These defined characters will be represented using sets. Examples: Input: â657â let us say regular expression contains the following characters- (â78653â) Output: Valid Exp 2 min read Check if a string exists in a PDF file in Python In this article, we'll learn how to use Python to determine whether a string is present in a PDF file. In Python, strings are essential for Projects, applications software, etc. Most of the time, we have to determine whether a string is present in a PDF file or not. Here, we'll discuss how to check 2 min read Python String rfind() Method Python String rfind() method returns the rightmost index of the substring if found in the given string. If not found then it returns -1.ExamplePythons = "GeeksForGeeks" print(s.rfind("Geeks"))Output8 Explanationstring "GeeksForGeeks" contains the substring "Geeks" twice.rfind() method starts the sea 4 min read How to check a valid regex string using Python? A Regex (Regular Expression) is a sequence of characters used for defining a pattern. This pattern could be used for searching, replacing and other operations. Regex is extensively utilized in applications that require input validation, Password validation, Pattern Recognition, search and replace ut 6 min read Like