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 Python set to check if string is pangram Given a string, check if the given string is a pangram or not. Examples: Input : The quick brown fox jumps over the lazy dog Output : The string is a pangram Input : geeks for geeks Output : The string is not pangram A normal way would have been to use frequency table and check if all elements were 2 min read Check for URL in a String - Python We are given a string that may contain one or more URLs and our task is to extract them efficiently. This is useful for web scraping, text processing, and data validation. For example:Input:s = "My Profile: https://auth.geeksforgeeks.org/user/Prajjwal%20/articles in the portal of https://www.geeksfo 3 min read Check If String is Integer in Python In this article, we will explore different possible ways through which we can check if a string is an integer or not. We will explore different methods and see how each method works with a clear understanding.Example:Input2 : "geeksforgeeks"Output2 : geeksforgeeks is not an IntigerExplanation : "gee 4 min read Using Set() in Python Pangram Checking Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Lowercase and Uppercase are considered the same. Examples: Input : str = 'The quick brown fox jumps over the lazy dog' Output : Yes // Contains all the characters from âaâ to âzâ In 3 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 SequenceMatcher in Python for Longest Common Substring Given two strings âXâ and âYâ, print the longest common sub-string. Examples: Input : X = "GeeksforGeeks", Y = "GeeksQuiz" Output : Geeks Input : X = "zxabcdezy", Y = "yzabcdezx" Output : abcdez We have existing solution for this problem please refer Print the longest common substring link. We will 2 min read Python - Replace all occurrences of a substring in a string Replacing all occurrences of a substring in a string means identifying every instance of a specific sequence of characters within a string and substituting it with another sequence of characters. Using replace()replace () method is the most straightforward and efficient way to replace all occurrence 2 min read Like