In this guide, we'll take a look at how to check if a string contains a substring in Python. As usual, each approach we'll cover has different pros and cons. Show
The in OperatorThe easiest way to check if a Python string contains a substring is to use the The
This operator is shorthand for calling an object's
To avoid this, you'll first want to check whether it points to
The String.index() MethodThe String type in Python has a method called Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! If the substring is not found, a
This method is useful if you need to know the position of the substring, as opposed to just its existence within the full string. The String.find() MethodThe String type has another method called If
If you'd prefer to avoid the need to catch errors, then this method should be favored over Regular Expressions (RegEx)Regular expressions provide a more flexible (albeit more complex) way to check strings for pattern matching. Python is shipped with a built-in module for regular expressions, called
This method is best if you are needing a more complex matching function, like case insensitive matching. Otherwise the complication and slower speed of regex should be avoided for simple substring matching use-cases. This article was written by Jacob Stopak, a software consultant and developer with passion for helping others improve their lives through code. Jacob is the creator of Initial Commit - a site dedicated to helping curious developers learn how their favorite programs are coded. Its featured project helps people learn Git at the code level. Check if String Contains Substring in PythonChecking if a string contains a substring is one of the most common tasks in any programming language. Python offers many ways to check if a string contains a substring. The simplest and fastest way to check whether a string contains a substring or not in Python is using the "in" operator , which is used as a comparison operator . Some other Python methods such as find(), index(), count() etc. also help to Check if a string contains a substring. Using Python's "in" operatorThe simplest and fastest way to check whether a string contains a substring or not in Python is the "in" operator . This operator returns true if the string contains the characters, otherwise, it returns false . outputPython "in" operator takes two arguments, one on the left and one on the right, and returns True if the left argument string is contained within the right argument string. More on "in" operatorNote: The "in" operator is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently. __contains__() functionPython String class has the __contains__() method that we can use to check if it contains another string or not. When we use Python "in" operator , internally it calls __contains__() function. The __contains__ method defines how instances of class behave when they appear at right side of in and not in operator. We can use this function directly too, but don't . Methods that start with underscores are considered semantically private , however it's recommended to use in operator for readability purposes. Using Python's str.find() methodAnother method you can use is the string.find() method . The find() method evaluate if the string contains a substring . If it does, the find() method returns the starting index of a substring within the string otherwise it returns -1 . outputMore on find() methodUsing str.find() method is the less Pythonic way , but it's still accepted. It's longer and a little bit more confusing, but it still gets the job done. Using Python Regular ExpressionRegular expression is widely used for pattern matching . Python has a built-in package called re , which can be used to work with Regular Expressions . The re module contains a function called search() , it can be used to check if a string contains the specified search pattern . exampleoutputUsing str.count() methodIf you want to count the number of occurrences of a specific substring in a string, then you can use Python count() method . If the substring is not found in a string, the function returns 0 . How do I check if a string contains text?You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.
How do I check if a string contains a character in Python?Using in operator
The Pythonic, fast way to check for the specific character in a string uses the in operator. It returns True if the character is found in the string and False otherwise. ch = '. '
How do you check if a word is in a sentence Python?If we want to check whether a given string contains a specified word in it or not, we can use the if/in statement in Python. The if/in statement returns True if the word is present in the string and False if the word is not in the string. The output shows that the word is is present inside the string variable string .
|