Python check if value in string

Python: Check if String Contains Substring

Checking whether a string contains a substring aids to generalize conditionals and create more flexible code. Additionally, depending on your domain model — checking if a string contains a substring may also allow you to infer fields of an object, if a string encodes a field in itself.

In this guide, we’ll take a look at how to check if a string contains a substring in Python.

The in Operator

The easiest way to check if a Python string contains a substring is to use the in operator.

The in operator is used to check data structures for membership in Python. It returns a Boolean (either True or False ). To check if a string contains a substring in Python using the in operator, we simply invoke it on the superstring:

fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!") 

This operator is shorthand for calling an object’s __contains__ method, and also works well for checking if an item exists in a list. It’s worth noting that it’s not null-safe, so if our fullstring was pointing to None , an exception would be thrown:

TypeError: argument of type 'NoneType' is not iterable 

To avoid this, you’ll first want to check whether it points to None or not:

fullstring = None substring = "tack" if fullstring != None and substring in fullstring: print("Found!") else: print("Not found!") 

The String.index() Method

The String type in Python has a method called index() that can be used to find the starting index of the first occurrence of a substring in a string.

Читайте также:  Php pdo exception 2002

If the substring is not found, a ValueError exception is thrown, which can be handled with a try-except-else block:

fullstring = "StackAbuse" substring = "tack" try: fullstring.index(substring) except ValueError: print("Not found!") else: print("Found!") 

This method is useful if you also need to know the position of the substring, as opposed to just its existence within the full string. The method itself returns the index:

print(fullstring.index(substring)) # 1 

Though — for the sake of checking whether a string contains a substring, this is a verbose approach.

The String.find() Method

The String class has another method called find() which is more convenient to use than index() , mainly because we don’t need to worry about handling any exceptions.

If find() doesn’t find a match, it returns -1, otherwise it returns the left-most index of the substring in the larger string:

Free eBook: Git Essentials

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!

fullstring = "StackAbuse" substring = "tack" if fullstring.find(substring) != -1: print("Found!") else: print("Not found!") 

Naturally, it performs the same search as index() and returns the index of the start of the substring within the parent string:

print(fullstring.find(substring)) # 1 

Regular Expressions (RegEx)

Regular expressions provide a more flexible (albeit more complex) way to check strings for pattern matching. With Regular Expressions, you can perform flexible and powerful searches through much larger search spaces, rather than simple checks, like previous ones.

Python is shipped with a built-in module for regular expressions, called re . The re module contains a function called search() , which we can use to match a substring pattern:

from re import search fullstring = "StackAbuse" substring = "tack" if search(substring, fullstring): print "Found!" else: print "Not found!" 

This method is best if you are needing a more complex matching function, like case insensitive matching, or if you’re dealing with large search spaces. Otherwise the complication and slower speed of regex should be avoided for simple substring matching use-cases.

About the Author

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.

Источник

Python String Contains: Check if a String Contains a Substring

Python String Contains Check if a String Contains a Substring Cover Image

In this tutorial, you’ll learn how to use Python to check if a string contains another substring. There are a number of ways to use Python to check a string for a substring, including case insensitivity, checking for patterns, and more. Being able to work with strings in Python is an important skill for a programmer of any skill level.

By the end of this tutorial, you’ll have learned:

  • How to use Python to check if a string contains a substring, using the in operator, the .index() method, the .find() method and more
  • How to ignore case sensitivity when checking if a Python string contains a substring
  • Filtering a Pandas DataFrame if the column contains a substring
  • Checking if a string contains a pattern substring using regex
  • And more

Checking if a Python String Contains a Substring with in

The simplest and most Pythonic way to check if a string in Python contains a substring is to use the in operator. The operator clearly expresses what you’re trying to accomplish and makes it clear to any reader of your code.

The in operator will return a boolean value: True if the substring is found in the string and False if it isn’t. Let’s take a look at an example:

# Using the in Operator to Check if a String Contains a Substring a_string = 'Welcome to datagy.io' print('datagy' in a_string) # Returns: True

In the example above, we used the in operator to check if the substring ‘datagy’ exists in the larger string.

Here, it’s important to note that the operator doesn’t check where the substring is, only that it exists. In the following section, you’ll learn how to identify where a substring exists in a larger string.

Checking if a Python String Contains a Substring with find

The Python find string method allows you to return the starting index of a substring, if it exists. This is analogous to many other languages that don’t explicitly support the in operator.

The string.find() method has two possibilities for return values:

  • A positive integer, representing the starting index of the first instance of the substring
  • -1 , if the substring doesn’t exist

Let’s take a look at how we can use the .find() method to check if a substring exists in a Python string:

# Using .find() to Check if a String Contains a Substring a_string = 'Welcome to datagy.io' if a_string.find('datagy') >= 0: print('String exists') else: print("String doesn't exist")

We can see that because the string contains the substring, it returns a positive index value. When a positive index value is returned, the if-else statement prints out that the string exists.

Checking if a Python String Contains a Substring with index

The Python string index method looks for a substring in a broader Python string and returns the beginning index, if the substring exists. This is very similar to the .find() method that we explored in the previous section.

One of the key differences between the Python .find() and .index() methods is that the .index() method will raise a ValueError if the substring is not found.

This means that in order to use this method, we can repeat our earlier example, except we wrap it in a try-except block instead. Let’s see how this works:

# Checking if a String Contains a Substring in Python a_string = 'Welcome to datagy.io' try: a_string.index('datagy') print('String exists') except ValueError: print("String doesn't exist")

Checking if a Python String Contains a Substring with count

The Python string count() method can be used to check if a string contains a substring by counting the number of times the substring exists in the broader string. The method will return the number times the substring exists. This means, that if the substring doesn’t exist, then the method will return 0.

Let’s see how we can use the .count() method to check if a Python string contains a substring:

# Using .count() to Check if a String Contains a Substring a_string = 'Welcome to datagy.io' if a_string.count('datagy'): print('String exists') else: print("String doesn't exist")

The reason that this works is that if a string doesn’t contain a substring, the count will be 0 . In Python, the value 0 evaluates to the boolean False . This means that the else block is triggered.

Filtering a List of Strings if They Contain a Substring

In this section, we’ll use the in operator to filter a list of strings if they contain a substring. In order to do this, we can use a for loop to loop over each item in the list to check if it contains a substring. If it does, then we append it to another list.

Let’s take a look at an example:

# Using a For Loop to Filter a List of Strings strings = ['hello and welcome', 'to the world of python', 'I am a python programmer', 'just learning'] filtered = [] for string in strings: if 'python' in string: filtered.append(string) print(filtered) # Returns: # ['to the world of python', 'I am a python programmer']

We can actually simplify this approach significantly by using a list comprehension, as shown below:

# Using a List Comprehension to Filter a List of Strings strings = ['hello and welcome', 'to the world of python', 'I am a python programmer', 'just learning'] filtered = [string for string in strings if 'python' in string] print(filtered) # Returns: # ['to the world of python', 'I am a python programmer']

Checking if a Python String Contains a Substring without Case Sensitivity

In this section, you’ll learn how to check if a Python string contains a substring without case sensitivity. The simplest way to do this is to check against both strings in their lowered string representations, using the .lower() method.

We can then use the in operator to check if the substring is in the larger string:

# Checking if a Python String Contains a Substring without Case Sensitivity string = 'Hello and welcome to DataGy' print('datagy'.lower() in string.lower()) # Returns: True

In the example above, we represent the substring and the broader string in their lower-case representations. This ensures that, regardless of case, the substring can be found if it exists.

Checking if a Python String Contains a Pattern of Text

In this section, you’ll learn how to check if a Python string contains a pattern of text. This can be helpful when you’re trying to see if, for example, a string contains an email address. We can do this by using the powerful regular expression library, re .

Let’s take a look at how we can do this using Python:

# Checking for a Substring in a Python String import re string = 'My email address is [email protected]' pattern = r'[\w\.-]+@[\w\.-]+' print(bool(re.search(pattern, string))) # Returns: True

In the example above, we create a pattern that checks for strings, periods and dashes, followed by a @ character, and preceded by the earlier pattern.

By converting the re.search() object to a boolean, the result will return True if the pattern is found and False if it isn’t.

Checking if a Pandas Column Contains a Substring

In this section, you’ll learn how to check if a Pandas column contains a substring. This can be incredibly helpful to filter a Pandas DataFrame based on a column containing a substring.

Pandas makes this easy using the str.contains() method, which checks if a string contains a substring. Let’s take a look at an example:

# Checking if a Pandas Column Contains a Substring import pandas as pd df = pd.DataFrame.from_dict(< 'a': [1, 2, 3, 4, 5], 'b': ['apple, banana', 'orange, banana', 'apple, orange', 'apple, banana', 'orange, banana'] >) print(df[df['b'].str.contains('orange')]) # Returns: # a b # 1 2 orange, banana # 2 3 apple, orange # 4 5 orange, banana

In the example above, we filter the Pandas DataFrame to only include rows where column ‘b’ contains the substring ‘orange’ .

Conclusion

In this tutorial, you learned how to use Python to check if a string contains a substring. There are a number of ways to accomplish this, though the most Pythonic and cleanest way is by using the in operator. That said, knowing how to use the .find() , .index() , and .count() methods can make you a stronger programmer.

You also learned how to check if a Python string contains a substring without case sensitivity and be able to check for a pattern in a broader string. Finally, you learned how to filter a Pandas DataFrame if a substring is found in a row.

Additional Resources

To learn more about related topics, check out the tutorials below:

Источник

Оцените статью