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 Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Like