Python if any word in string

I’m working with Python, and I’m trying to find out if you can tell if a word is in a string.

I have found some information about identifying if the word is in the string — using .find, but is there a way to do an if statement. I would like to have something like the following:

if string.find(word):
    print("success")

mkrieger1's user avatar

mkrieger1

17.7k4 gold badges54 silver badges62 bronze badges

asked Mar 16, 2011 at 1:10

The Woo's user avatar

0

What is wrong with:

if word in mystring: 
   print('success')

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Mar 16, 2011 at 1:13

fabrizioM's user avatar

fabrizioMfabrizioM

46k15 gold badges100 silver badges118 bronze badges

13

if 'seek' in 'those who seek shall find':
    print('Success!')

but keep in mind that this matches a sequence of characters, not necessarily a whole word — for example, 'word' in 'swordsmith' is True. If you only want to match whole words, you ought to use regular expressions:

import re

def findWholeWord(w):
    return re.compile(r'b({0})b'.format(w), flags=re.IGNORECASE).search

findWholeWord('seek')('those who seek shall find')    # -> <match object>
findWholeWord('word')('swordsmith')                   # -> None

answered Mar 16, 2011 at 1:52

Hugh Bothwell's user avatar

Hugh BothwellHugh Bothwell

54.7k8 gold badges84 silver badges99 bronze badges

6

If you want to find out whether a whole word is in a space-separated list of words, simply use:

def contains_word(s, w):
    return (' ' + w + ' ') in (' ' + s + ' ')

contains_word('the quick brown fox', 'brown')  # True
contains_word('the quick brown fox', 'row')    # False

This elegant method is also the fastest. Compared to Hugh Bothwell’s and daSong’s approaches:

>python -m timeit -s "def contains_word(s, w): return (' ' + w + ' ') in (' ' + s + ' ')" "contains_word('the quick brown fox', 'brown')"
1000000 loops, best of 3: 0.351 usec per loop

>python -m timeit -s "import re" -s "def contains_word(s, w): return re.compile(r'b({0})b'.format(w), flags=re.IGNORECASE).search(s)" "contains_word('the quick brown fox', 'brown')"
100000 loops, best of 3: 2.38 usec per loop

>python -m timeit -s "def contains_word(s, w): return s.startswith(w + ' ') or s.endswith(' ' + w) or s.find(' ' + w + ' ') != -1" "contains_word('the quick brown fox', 'brown')"
1000000 loops, best of 3: 1.13 usec per loop

Edit: A slight variant on this idea for Python 3.6+, equally fast:

def contains_word(s, w):
    return f' {w} ' in f' {s} '

answered Apr 11, 2016 at 20:32

user200783's user avatar

user200783user200783

13.6k11 gold badges67 silver badges132 bronze badges

6

You can split string to the words and check the result list.

if word in string.split():
    print("success")

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Dec 1, 2016 at 18:26

Corvax's user avatar

CorvaxCorvax

7647 silver badges12 bronze badges

3

find returns an integer representing the index of where the search item was found. If it isn’t found, it returns -1.

haystack = 'asdf'

haystack.find('a') # result: 0
haystack.find('s') # result: 1
haystack.find('g') # result: -1

if haystack.find(needle) >= 0:
  print('Needle found.')
else:
  print('Needle not found.')

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Mar 16, 2011 at 1:13

Matt Howell's user avatar

Matt HowellMatt Howell

15.6k7 gold badges48 silver badges56 bronze badges

0

This small function compares all search words in given text. If all search words are found in text, returns length of search, or False otherwise.

Also supports unicode string search.

def find_words(text, search):
    """Find exact words"""
    dText   = text.split()
    dSearch = search.split()

    found_word = 0

    for text_word in dText:
        for search_word in dSearch:
            if search_word == text_word:
                found_word += 1

    if found_word == len(dSearch):
        return lenSearch
    else:
        return False

usage:

find_words('çelik güray ankara', 'güray ankara')

Trang Oul's user avatar

answered Jun 22, 2012 at 22:51

Guray Celik's user avatar

Guray CelikGuray Celik

1,2811 gold badge14 silver badges13 bronze badges

0

If matching a sequence of characters is not sufficient and you need to match whole words, here is a simple function that gets the job done. It basically appends spaces where necessary and searches for that in the string:

def smart_find(haystack, needle):
    if haystack.startswith(needle+" "):
        return True
    if haystack.endswith(" "+needle):
        return True
    if haystack.find(" "+needle+" ") != -1:
        return True
    return False

This assumes that commas and other punctuations have already been stripped out.

IanS's user avatar

IanS

15.6k9 gold badges59 silver badges84 bronze badges

answered Jun 15, 2012 at 7:23

daSong's user avatar

daSongdaSong

4071 gold badge5 silver badges9 bronze badges

1

Using regex is a solution, but it is too complicated for that case.

You can simply split text into list of words. Use split(separator, num) method for that. It returns a list of all the words in the string, using separator as the separator. If separator is unspecified it splits on all whitespace (optionally you can limit the number of splits to num).

list_of_words = mystring.split()
if word in list_of_words:
    print('success')

This will not work for string with commas etc. For example:

mystring = "One,two and three"
# will split into ["One,two", "and", "three"]

If you also want to split on all commas etc. use separator argument like this:

# whitespace_chars = " tnrf" - space, tab, newline, return, formfeed
list_of_words = mystring.split( tnrf,.;!?'"()")
if word in list_of_words:
    print('success')

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Dec 18, 2017 at 11:44

tstempko's user avatar

tstempkotstempko

1,1761 gold badge15 silver badges17 bronze badges

2

As you are asking for a word and not for a string, I would like to present a solution which is not sensitive to prefixes / suffixes and ignores case:

#!/usr/bin/env python

import re


def is_word_in_text(word, text):
    """
    Check if a word is in a text.

    Parameters
    ----------
    word : str
    text : str

    Returns
    -------
    bool : True if word is in text, otherwise False.

    Examples
    --------
    >>> is_word_in_text("Python", "python is awesome.")
    True

    >>> is_word_in_text("Python", "camelCase is pythonic.")
    False

    >>> is_word_in_text("Python", "At the end is Python")
    True
    """
    pattern = r'(^|[^w]){}([^w]|$)'.format(word)
    pattern = re.compile(pattern, re.IGNORECASE)
    matches = re.search(pattern, text)
    return bool(matches)


if __name__ == '__main__':
    import doctest
    doctest.testmod()

If your words might contain regex special chars (such as +), then you need re.escape(word)

answered Aug 9, 2017 at 10:11

Martin Thoma's user avatar

Martin ThomaMartin Thoma

121k154 gold badges603 silver badges926 bronze badges

Advanced way to check the exact word, that we need to find in a long string:

import re
text = "This text was of edited by Rock"
#try this string also
#text = "This text was officially edited by Rock" 
for m in re.finditer(r"bofb", text):
    if m.group(0):
        print("Present")
    else:
        print("Absent")

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Nov 2, 2016 at 8:39

Rameez's user avatar

RameezRameez

5545 silver badges11 bronze badges

What about to split the string and strip words punctuation?

w in [ws.strip(',.?!') for ws in p.split()]

If need, do attention to lower/upper case:

w.lower() in [ws.strip(',.?!') for ws in p.lower().split()]

Maybe that way:

def wcheck(word, phrase):
    # Attention about punctuation and about split characters
    punctuation = ',.?!'
    return word.lower() in [words.strip(punctuation) for words in phrase.lower().split()]

Sample:

print(wcheck('CAr', 'I own a caR.'))

I didn’t check performance…

answered Dec 26, 2020 at 5:18

marcio's user avatar

marciomarcio

5067 silver badges19 bronze badges

You could just add a space before and after «word».

x = raw_input("Type your word: ")
if " word " in x:
    print("Yes")
elif " word " not in x:
    print("Nope")

This way it looks for the space before and after «word».

>>> Type your word: Swordsmith
>>> Nope
>>> Type your word:  word 
>>> Yes

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Feb 26, 2015 at 14:23

PyGuy's user avatar

PyGuyPyGuy

433 bronze badges

1

I believe this answer is closer to what was initially asked: Find substring in string but only if whole words?

It is using a simple regex:

import re

if re.search(r"b" + re.escape(word) + r"b", string):
  print('success')

Martin Thoma's user avatar

Martin Thoma

121k154 gold badges603 silver badges926 bronze badges

answered Aug 25, 2021 at 13:25

Milos Cuculovic's user avatar

Milos CuculovicMilos Cuculovic

19.4k50 gold badges159 silver badges264 bronze badges

One of the solutions is to put a space at the beginning and end of the test word. This fails if the word is at the beginning or end of a sentence or is next to any punctuation. My solution is to write a function that replaces any punctuation in the test string with spaces, and add a space to the beginning and end or the test string and test word, then return the number of occurrences. This is a simple solution that removes the need for any complex regex expression.

def countWords(word, sentence):
    testWord = ' ' + word.lower() + ' '
    testSentence = ' '

    for char in sentence:
        if char.isalpha():
            testSentence = testSentence + char.lower()
        else:
            testSentence = testSentence + ' '

    testSentence = testSentence + ' '

    return testSentence.count(testWord)

To count the number of occurrences of a word in a string:

sentence = "A Frenchman ate an apple"
print(countWords('a', sentence))

returns 1

sentence = "Is Oporto a 'port' in Portugal?"
print(countWords('port', sentence))

returns 1

Use the function in an ‘if’ to test if the word exists in a string

answered Mar 18, 2022 at 9:37

iStuart's user avatar

iStuartiStuart

3953 silver badges6 bronze badges

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Check if a Python String Contains a Substring

If you’re new to programming or come from a programming language other than Python, you may be looking for the best way to check whether a string contains another string in Python.

Identifying such substrings comes in handy when you’re working with text content from a file or after you’ve received user input. You may want to perform different actions in your program depending on whether a substring is present or not.

In this tutorial, you’ll focus on the most Pythonic way to tackle this task, using the membership operator in. Additionally, you’ll learn how to identify the right string methods for related, but different, use cases.

Finally, you’ll also learn how to find substrings in pandas columns. This is helpful if you need to search through data from a CSV file. You could use the approach that you’ll learn in the next section, but if you’re working with tabular data, it’s best to load the data into a pandas DataFrame and search for substrings in pandas.

How to Confirm That a Python String Contains Another String

If you need to check whether a string contains a substring, use Python’s membership operator in. In Python, this is the recommended way to confirm the existence of a substring in a string:

>>>

>>> raw_file_content = """Hi there and welcome.
... This is a special hidden file with a SECRET secret.
... I don't want to tell you The Secret,
... but I do want to secretly tell you that I have one."""

>>> "secret" in raw_file_content
True

The in membership operator gives you a quick and readable way to check whether a substring is present in a string. You may notice that the line of code almost reads like English.

When you use in, the expression returns a Boolean value:

  • True if Python found the substring
  • False if Python didn’t find the substring

You can use this intuitive syntax in conditional statements to make decisions in your code:

>>>

>>> if "secret" in raw_file_content:
...    print("Found!")
...
Found!

In this code snippet, you use the membership operator to check whether "secret" is a substring of raw_file_content. If it is, then you’ll print a message to the terminal. Any indented code will only execute if the Python string that you’re checking contains the substring that you provide.

The membership operator in is your best friend if you just need to check whether a Python string contains a substring.

However, what if you want to know more about the substring? If you read through the text stored in raw_file_content, then you’ll notice that the substring occurs more than once, and even in different variations!

Which of these occurrences did Python find? Does capitalization make a difference? How often does the substring show up in the text? And what’s the location of these substrings? If you need the answer to any of these questions, then keep on reading.

Generalize Your Check by Removing Case Sensitivity

Python strings are case sensitive. If the substring that you provide uses different capitalization than the same word in your text, then Python won’t find it. For example, if you check for the lowercase word "secret" on a title-case version of the original text, the membership operator check returns False:

>>>

>>> title_cased_file_content = """Hi There And Welcome.
... This Is A Special Hidden File With A Secret Secret.
... I Don't Want To Tell You The Secret,
... But I Do Want To Secretly Tell You That I Have One."""

>>> "secret" in title_cased_file_content
False

Despite the fact that the word secret appears multiple times in the title-case text title_cased_file_content, it never shows up in all lowercase. That’s why the check that you perform with the membership operator returns False. Python can’t find the all-lowercase string "secret" in the provided text.

Humans have a different approach to language than computers do. This is why you’ll often want to disregard capitalization when you check whether a string contains a substring in Python.

You can generalize your substring check by converting the whole input text to lowercase:

>>>

>>> file_content = title_cased_file_content.lower()

>>> print(file_content)
hi there and welcome.
this is a special hidden file with a secret secret.
i don't want to tell you the secret,
but i do want to secretly tell you that i have one.

>>> "secret" in file_content
True

Converting your input text to lowercase is a common way to account for the fact that humans think of words that only differ in capitalization as the same word, while computers don’t.

Now that you’ve converted the string to lowercase to avoid unintended issues stemming from case sensitivity, it’s time to dig further and learn more about the substring.

Learn More About the Substring

The membership operator in is a great way to descriptively check whether there’s a substring in a string, but it doesn’t give you any more information than that. It’s perfect for conditional checks—but what if you need to know more about the substrings?

Python provides many additonal string methods that allow you to check how many target substrings the string contains, to search for substrings according to elaborate conditions, or to locate the index of the substring in your text.

In this section, you’ll cover some additional string methods that can help you learn more about the substring.

By using in, you confirmed that the string contains the substring. But you didn’t get any information on where the substring is located.

If you need to know where in your string the substring occurs, then you can use .index() on the string object:

>>>

>>> file_content = """hi there and welcome.
... this is a special hidden file with a secret secret.
... i don't want to tell you the secret,
... but i do want to secretly tell you that i have one."""

>>> file_content.index("secret")
59

When you call .index() on the string and pass it the substring as an argument, you get the index position of the first character of the first occurrence of the substring.

But what if you want to find other occurrences of the substring? The .index() method also takes a second argument that can define at which index position to start looking. By passing specific index positions, you can therefore skip over occurrences of the substring that you’ve already identified:

>>>

>>> file_content.index("secret", 60)
66

When you pass a starting index that’s past the first occurrence of the substring, then Python searches starting from there. In this case, you get another match and not a ValueError.

That means that the text contains the substring more than once. But how often is it in there?

You can use .count() to get your answer quickly using descriptive and idiomatic Python code:

>>>

>>> file_content.count("secret")
4

You used .count() on the lowercase string and passed the substring "secret" as an argument. Python counted how often the substring appears in the string and returned the answer. The text contains the substring four times. But what do these substrings look like?

You can inspect all the substrings by splitting your text at default word borders and printing the words to your terminal using a for loop:

>>>

>>> for word in file_content.split():
...    if "secret" in word:
...        print(word)
...
secret
secret.
secret,
secretly

In this example, you use .split() to separate the text at whitespaces into strings, which Python packs into a list. Then you iterate over this list and use in on each of these strings to see whether it contains the substring "secret".

Now that you can inspect all the substrings that Python identifies, you may notice that Python doesn’t care whether there are any characters after the substring "secret" or not. It finds the word whether it’s followed by whitespace or punctuation. It even finds words such as "secretly".

That’s good to know, but what can you do if you want to place stricter conditions on your substring check?

Find a Substring With Conditions Using Regex

You may only want to match occurrences of your substring followed by punctuation, or identify words that contain the substring plus other letters, such as "secretly".

For such cases that require more involved string matching, you can use regular expressions, or regex, with Python’s re module.

For example, if you want to find all the words that start with "secret" but are then followed by at least one additional letter, then you can use the regex word character (w) followed by the plus quantifier (+):

>>>

>>> import re

>>> file_content = """hi there and welcome.
... this is a special hidden file with a secret secret.
... i don't want to tell you the secret,
... but i do want to secretly tell you that i have one."""

>>> re.search(r"secretw+", file_content)
<re.Match object; span=(128, 136), match='secretly'>

The re.search() function returns both the substring that matched the condition as well as its start and end index positions—rather than just True!

You can then access these attributes through methods on the Match object, which is denoted by m:

>>>

>>> m = re.search(r"secretw+", file_content)

>>> m.group()
'secretly'

>>> m.span()
(128, 136)

These results give you a lot of flexibility to continue working with the matched substring.

For example, you could search for only the substrings that are followed by a comma (,) or a period (.):

>>>

>>> re.search(r"secret[.,]", file_content)
<re.Match object; span=(66, 73), match='secret.'>

There are two potential matches in your text, but you only matched the first result fitting your query. When you use re.search(), Python again finds only the first match. What if you wanted all the mentions of "secret" that fit a certain condition?

To find all the matches using re, you can work with re.findall():

>>>

>>> re.findall(r"secret[.,]", file_content)
['secret.', 'secret,']

By using re.findall(), you can find all the matches of the pattern in your text. Python saves all the matches as strings in a list for you.

When you use a capturing group, you can specify which part of the match you want to keep in your list by wrapping that part in parentheses:

>>>

>>> re.findall(r"(secret)[.,]", file_content)
['secret', 'secret']

By wrapping secret in parentheses, you defined a single capturing group. The findall() function returns a list of strings matching that capturing group, as long as there’s exactly one capturing group in the pattern. By adding the parentheses around secret, you managed to get rid of the punctuation!

Using re.findall() with match groups is a powerful way to extract substrings from your text. But you only get a list of strings, which means that you’ve lost the index positions that you had access to when you were using re.search().

If you want to keep that information around, then re can give you all the matches in an iterator:

>>>

>>> for match in re.finditer(r"(secret)[.,]", file_content):
...    print(match)
...
<re.Match object; span=(66, 73), match='secret.'>
<re.Match object; span=(103, 110), match='secret,'>

When you use re.finditer() and pass it a search pattern and your text content as arguments, you can access each Match object that contains the substring, as well as its start and end index positions.

You may notice that the punctuation shows up in these results even though you’re still using the capturing group. That’s because the string representation of a Match object displays the whole match rather than just the first capturing group.

But the Match object is a powerful container of information and, like you’ve seen earlier, you can pick out just the information that you need:

>>>

>>> for match in re.finditer(r"(secret)[.,]", file_content):
...    print(match.group(1))
...
secret
secret

By calling .group() and specifying that you want the first capturing group, you picked the word secret without the punctuation from each matched substring.

You can go into much more detail with your substring matching when you use regular expressions. Instead of just checking whether a string contains another string, you can search for substrings according to elaborate conditions.

Using regular expressions with re is a good approach if you need information about the substrings, or if you need to continue working with them after you’ve found them in the text. But what if you’re working with tabular data? For that, you’ll turn to pandas.

Find a Substring in a pandas DataFrame Column

If you work with data that doesn’t come from a plain text file or from user input, but from a CSV file or an Excel sheet, then you could use the same approach as discussed above.

However, there’s a better way to identify which cells in a column contain a substring: you’ll use pandas! In this example, you’ll work with a CSV file that contains fake company names and slogans. You can download the file below if you want to work along:

When you’re working with tabular data in Python, it’s usually best to load it into a pandas DataFrame first:

>>>

>>> import pandas as pd

>>> companies = pd.read_csv("companies.csv")

>>> companies.shape
(1000, 2)

>>> companies.head()
             company                                     slogan
0      Kuvalis-Nolan      revolutionize next-generation metrics
1  Dietrich-Champlin  envisioneer bleeding-edge functionalities
2           West Inc            mesh user-centric infomediaries
3         Wehner LLC               utilize sticky infomediaries
4      Langworth Inc                 reinvent magnetic networks

In this code block, you loaded a CSV file that contains one thousand rows of fake company data into a pandas DataFrame and inspected the first five rows using .head().

After you’ve loaded the data into the DataFrame, you can quickly query the whole pandas column to filter for entries that contain a substring:

>>>

>>> companies[companies.slogan.str.contains("secret")]
              company                                  slogan
7          Maggio LLC                    target secret niches
117      Kub and Sons              brand secret methodologies
654       Koss-Zulauf              syndicate secret paradigms
656      Bernier-Kihn  secretly synthesize back-end bandwidth
921      Ward-Shields               embrace secret e-commerce
945  Williamson Group             unleash secret action-items

You can use .str.contains() on a pandas column and pass it the substring as an argument to filter for rows that contain the substring.

When you’re working with .str.contains() and you need more complex match scenarios, you can also use regular expressions! You just need to pass a regex-compliant search pattern as the substring argument:

>>>

>>> companies[companies.slogan.str.contains(r"secretw+")]
          company                                  slogan
656  Bernier-Kihn  secretly synthesize back-end bandwidth

In this code snippet, you’ve used the same pattern that you used earlier to match only words that contain secret but then continue with one or more word character (w+). Only one of the companies in this fake dataset seems to operate secretly!

You can write any complex regex pattern and pass it to .str.contains() to carve from your pandas column just the rows that you need for your analysis.

Conclusion

Like a persistent treasure hunter, you found each "secret", no matter how well it was hidden! In the process, you learned that the best way to check whether a string contains a substring in Python is to use the in membership operator.

You also learned how to descriptively use two other string methods, which are often misused to check for substrings:

  • .count() to count the occurrences of a substring in a string
  • .index() to get the index position of the beginning of the substring

After that, you explored how to find substrings according to more advanced conditions with regular expressions and a few functions in Python’s re module.

Finally, you also learned how you can use the DataFrame method .str.contains() to check which entries in a pandas DataFrame contain a substring .

You now know how to pick the most idiomatic approach when you’re working with substrings in Python. Keep using the most descriptive method for the job, and you’ll write code that’s delightful to read and quick for others to understand.

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Check if a Python String Contains a Substring

  1. HowTo
  2. Python How-To’s
  3. Check if a String Contains Word in …

Muhammad Maisam Abbas
Dec 21, 2022
Jun 07, 2021

Check if a String Contains Word in Python

This tutorial will introduce the method to find whether a specified word is inside a string variable or not in Python.

Check the String if It Contains a Word Through an if/in Statement in 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 following program snippet shows us how to use the if/in statement to determine whether a string contains a word or not:

string = "This contains a word"
if "word" in string:
    print("Found")
else:
    print("Not Found")

Output:

We checked whether the string variable string contains the word word inside it or not with the if/in statement in the program above. This approach compares both strings character-wise; this means that it doesn’t compare whole words and can give us wrong answers, as demonstrated in the following example:

string = "This contains a word"
if "is" in string:
    print("Found")
else:
    print("Not Found")

Output:

The output shows that the word is is present inside the string variable string. But, in reality, this is is just a part of the first word This in the string variable.

This problem has a simple solution. We can surround the word and the string variable with white spaces to just compare the whole word. The program below shows us how we can do that:

string = "This contains a word"
if " is " in (" " + string + " "):
    print("Found")
else:
    print("Not Found")

Output:

In the code above, we used the same if/in statement, but we slightly altered it to compare only individual words. This time, the output shows no such word as is present inside the string variable.

Muhammad Maisam Abbas avatar
Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article — Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Extract Numbers From a String in Python
  • Convert String to Datetime in Python

Ezoic

Knowing how to check if a Python string contains a substring is a very common thing we do in our programs.

In how many ways can you do this check?

Python provides multiple ways to check if a string contains a substring. Some ways are: the in operator, the index method, the find method, the use of a regular expressions.

In this tutorial you will learn multiple ways to find out if a substring is part of a string. This will also give you the understanding of how to solve the same problem in multiple ways using Python.

Let’s get started!

The first option available in Python is the in operator.

>>> 'This' in 'This is a string'
True
>>> 'this' in 'This is a string'
False
>>> 

As you can see the in operator returns True if the string on its left is part of the string on its right. Otherwise it returns False.

This expression can be used as part of an if else statement:

>>> if 'This' in 'This is a string':
...     print('Substring found')
... else:
...     print('Substring not found')
... 
Substring found

To reverse the logic of this if else statement you can add the not operator.

>>> if 'This' not in 'This is a string':
...     print('Substring not found')
... else:
...     print('Substring found')
... 
Substring found

You can also use the in operator to check if a Python list contains a specific item.

Index Method For Python Strings

I want to see how else I can find out if a substring is part of a string in Python.

One way to do that is by looking at the methods available for string data types in Python using the following command in the Python shell:

>>> help(str)

In the output of the help command you will see that one of the methods we can use to find out if a substring is part of a string is the index method.

The string index method in Python returns the index in our string where the substring is found, otherwise it raises a ValueError exception

Let’s see an example:

>>> 'This is a string'.index('This')
0
>>> 'This is a string'.index('is a')
5
>>> 'This is a string'.index('not present')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found

In the first example the index method returns 0 because the string ‘This’ is found at index zero of our string.

The second example returns 5 because that’s where the string ‘is a’ is found (considering that we start counting indexes from zero).

In the third example the Python interpreter raises a ValueError exception because the string ‘not present’ is not found in our string.

The advantage of this method over the in operator is that the index method not only tells us that a substring is part of a string. It also tells us at which index the substring starts.

Find Method For Python Strings

While looking at the help page for strings in Python I can see another method available that seems to be similar to the index method. It’s the find method.

The string find method in Python returns the index at which a substring is found in a string. It returns -1 if the substring is not found.

Let’s run the same three examples we have used to show the index method:

>>> 'This is a string'.find('This')
0
>>> 'This is a string'.find('is a')
5
>>> 'This is a string'.find('not present')
-1

As you can see the output of the first two examples is identical. The only one that changes is the third example for a substring that is not present in our string.

In this scenario the find method returns -1 instead of raising a ValueError exception like the index method does.

The find method is easier to use than the index method because with it we don’t have to handle exceptions in case a substring is not part of a string.

Python String __contains__ Method

I wonder how the in operator works behind the scenes, to understand that let’s start by creating a new string and by looking at its attributes and methods using the dir function:

>>> test_string = 'This is a string'
>>> dir(test_string)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

I want to focus your attention on the __contains__ method.

Let’s see if we can use it to check if a substring is part of a string:

>>> test_string.__contains__('This')
True
>>> test_string.__contains__('this')
False

It works in the same way the in operator does.

So, what’s the difference between the two?

Considering that the method name starts with double underscore “__”, the method should be considered “private” and we shouldn’t call it directly.

The __contains__ method is called indirectly when you use the in operator.

It’s something handy to know! 🙂

Search For Substring in a Python String Using a Regular Expression

If we go back to the in operator I want to verify how the operator behaves if I want to perform a case insensitive check.

>>> 'this' in 'This is a string'
False

This time the in operator returns False because the substring ‘this’ (starting with lower case t) is not part of our string.

But what if I want to know if a substring is part of a string no matter if it’s lower or upper case?

How can I do that?

I could still use the in operator together with a logical or:

>>> 'This' in 'This is a string' or 'this' in 'This is a string'
True

As you can see the expression works but it can become quite long and difficult to read.

Imagine if you want to match ‘This’, ‘this’, ‘THIS’…etc..basically all the combinations of lower and uppercase letters. It would be a nightmare!

An alternative is provided by the Python built-in module re (for regular expressions) that can be used to find out if a specific pattern is included in a string.

The re module provides a function called search that can help us in this case…

Let’s import the re module and look at the help for the search function:

>>> import re
>>> help(re.search)

Using the search function our initial example becomes:

>>> import re
>>> re.search('This', 'This is a string')
<re.Match object; span=(0, 4), match='This'>

We get back a re.Match object?!?

What can we do with it? Let’s try to convert it into a boolean…

>>> bool(re.search('This', 'This is a string'))
True
>>> bool(re.search('Thiz', 'This is a string'))
False

You can see that we get True and False results in line with the search we are doing. The re.search function is doing what we expect.

Let’s see if I can use this expression as part of an if else statement:

>>> if re.search('This', 'This is a string'):
...     print('Substring found')
... else:
...     print('Substring not found')
... 
Substring found

>>> 
>>> if re.search('Thiz', 'This is a string'):
...     print('Substring found')
... else:
...     print('Substring not found')
... 
Substring not found

It works with an if else statement too. Good to know 🙂

Insensitive Search For Substring in a Python String

But what about the insensitive check we were talking about before?

Try to run the following…

>>> re.search('this', 'This is a string')

…you will see that it doesn’t return any object. In other words the substring ‘this’ is not found in our string.

We have the option to pass an additional argument to the search function, a flag to force a case insensitive check (have a look at the help for the search function above, it’s right there).

The name of the flag for case insensitive matching is re.IGNORECASE.

>>> re.search('this', 'This is a string', re.IGNORECASE)
<re.Match object; span=(0, 4), match='This'>

This time we get an object back. Nice!

Check If a Python String Contains Multiple Substrings

It’s very common having to check if a string contains multiple substrings.

Imagine you have a document and you want to confirm, given a list of words, which ones are part of the document.

In this example we are using a short string but imagine the string being a document of any length.

document = "The Python programming language was created by Guido van Rossum"
words = ["Python", "Rossum", "substring"]

We want to find out which elements of the list words are inside the string document.

Let’s start with the most obvious implementation using a for loop:

words_found = []

for word in words:
    if word in document:
        words_found.append(word)

Here is the content of the list words_found:

>>> words_found
['Python', 'Rossum']

But, what happens if the list words contains duplicates?

words = ["Python", "Rossum", "substring", "Python"]

In this case the list words_found contains duplicates too:

>>> words_found
['Python', 'Rossum', 'Python']

To eliminate duplicates from the list of substrings found in the document string, we can add a condition to the if statement that checks if a word is already in the list words_found before adding it to it:

words_found = []

for word in words:
    if word in document and word not in words_found:
        words_found.append(word)

This time the output is the following (it doesn’t contain any duplicates):

>>> words_found
['Python', 'Rossum']

Checking For Multiple Substrings in a String Using a List or Set Comprehension

How can we do the same check implemented in the previous section but using more concise code?

One great option that Python provides are list comprehensions.

I can find out which words are part of my document using the following expression:

>>> words_found = [word for word in words if word in document]
>>> words_found
['Python', 'Rossum', 'Python']

That’s pretty cool!

A single line to do that same thing we have done before with four lines.

Wondering how we can remove duplicates also in this case?

I could convert the list returned by the list comprehension into a set that by definition has unique elements:

>>> words_found = set([word for word in words if word in document])
>>> words_found
{'Rossum', 'Python'}

Also, in case you are not aware, Python provides set comprehensions. Their syntax is the same as list comprehensions with the difference that square brackets are replaced by curly brackets:

>>> words_found = {word for word in words if word in document}
>>> words_found
{'Rossum', 'Python'}

Makes sense?

Check If a String Contains Any or All Elements in a List

Now, let’s say we only want to know if any of the elements in the list words is inside the string document.

To do that we can use the any() function.

The any() function is applicable to iterables. It returns True if any of the items in the iterable is True, otherwise it returns False. It also returns False if the iterable is empty.

Once again, here are the variables we are using in this example:

document = "The Python programming language was created by Guido van Rossum"
words = ["Python", "Rossum", "substring"]

In the previous section we have used the following list comprehension that returns the words inside our string:

words_found = [word for word in words if word in document]

Now, we will do something slightly different. I want to know if each word in the words list is in the document string or not.

Basically I want as a result a list that contains True or False and that tells us if a specific word is in the string document or not.

To do that we can change our list comprehension…

…this time we want a list comprehension with boolean elements:

>>> [word in document for word in words]
[True, True, False]

The first two items of the list returned by the list comprehension are True because the words “Python” and “Rossum” are in the string document.

Based on the same logic, do you see why the third item is False?

Now I can apply the any function to the output of our list comprehension to check if at least one of the words is inside our string:

>>> any([word in document for word in words])
True

As expected the result is True (based on the definition of the any function I have given at the beginning of this section).

Before moving to the next section I want to quickly cover the all() function.

The all() function is applicable to iterables. It returns True if all the items in the iterable are True, otherwise it returns False. It also returns True if the iterable is empty.

If we apply the all() function to our previous list comprehension we expect False as result considering that one of the three items in the list is False:

>>> all([word in document for word in words])
False

All clear?

Identify Multiple String Matches with a Regular Expression

We can also verify if substrings in a list are part of a string using a regular expression.

This approach is not simpler than other approaches we have seen so far. But, at the same time, it’s another tool that you can add to your Python knowledge.

As explained before to use regular expressions in our Python program we have to import the re module.

The findall() function, part of the re module, returns matches of a specific pattern in a string as a list of strings.

In this case the list of strings returned will contain the words found in the string document.

import re

document = "The Python programming language was created by Guido van Rossum"
words = ["Python", "Rossum", "substring"]

re.findall('Python|Rossum|substring', document, re.IGNORECASE)

As you can see we have used the or logical expression to match any of the items in the list words.

The output is:

['Python', 'Rossum']

But imagine if the list words contained hundreds of items. It would be impossible to specify each one of them in the regular expression.

So, what can we do instead?

We can use the following expression, simplified due to the string join() method.

>>> re.findall('|'.join(words), document, re.IGNORECASE)
['Python', 'Rossum']

And here is the final version of our program that applies the any() function to the output of the re.findall function.

import re

document = "The Python programming language was created by Guido van Rossum"
words = ["Python", "Rossum", "substring"]

if any(re.findall('|'.join(words), document, re.IGNORECASE)):
    print("Match found")
else:
    print("No match found")

Find the First Match in a String From a Python List

Before completing this tutorial I will show you how, given a list of words, you can find out the first match in a string.

Let’s go back to the following list comprehension:

[word for word in words if word in document]

A simple way to find out the first match is by using the Python next() function.

The Python next() function returns the next item in an iterator. It also allows to provide a default value returned when the end of the iterator is reached.

Let’s apply the next function multiple times to our list comprehension to see what we get back:

>>> next([word for word in words if word in document])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not an iterator

Interesting, we are seeing a TypeError exception being raised by the Python interpreter.

Do you know why?

The answer is in the error…

A list comprehension returns a list, and a list is not an iterator. And as I said before the next() function can only be applied to an iterator.

In Python you can define an iterator using parentheses instead of square brackets:

>>> (word for word in words if word in document)
<generator object <genexpr> at 0x10c3e8450>

Let’s apply the next() function multiple times to the iterator, to understand what this function returns:

>>> matches = (word for word in words if word in document)
>>> next(matches)
'Python'
>>> next(matches)
'Rossum'
>>> next(matches)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

As explained at the beginning of this section we can also provide a default value that is returned when we reach the end of the iterator.

>>> matches = (word for word in words if word in document)
>>> next(matches, "No more elements")
'Python'
>>> next(matches, "No more elements")
'Rossum'
>>> next(matches, "No more elements")
'No more elements'

Going back to what we wanted to achieve at the beginning of this section…

Here is how we can get the first match in our string document:

document = "The Python programming language was created by Guido van Rossum"
words = ["Python", "Rossum", "substring"]

first_match = next((word for word in words if word in document), "No more elements")
print(first_match)

Conclusion

We have started by looking at three different ways to check if a substring is part of a string:

  • Using the in operator that returns a boolean to say if the substring is present in the string.
  • With the index method that returns the index at which the substring is found or raises a ValueError if the substring is not in the string.
  • Using the find method that behaves like the index method with the only difference that it returns -1 if the substring is not part of the string.

You have also seen how to find out if a string contains multiple substrings using few different techniques based on list comprehensions, set comprehensions, any() / all() functions and regular expressions.

And now that you have seen all these alternatives you have…

…which one is your favourite? 🙂

I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

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.

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:

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.

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 google sheets to excel
  • Python function in excel
  • Python and excel macros
  • Python and excel files
  • Python and excel example