You can use regular expressions and the word boundary special character \b (highlight by me):

Matches the empty string, but only at the beginning or end of a word. A word is defined as a sequence of alphanumeric or underscore characters, so the end of a word is indicated by whitespace or a non-alphanumeric, non-underscore character. Note that \b is defined as the boundary between \w and \W, so the precise set of characters deemed to be alphanumeric depends on the values of the UNICODE and LOCALE flags. Inside a character range, \b represents the backspace character, for compatibility with Python’s string literals.

def string_found(string1, string2):
    if re.search(r"\b" + re.escape(string1) + r"\b", string2):
        return True
    return False

Demo


If word boundaries are only whitespaces for you, you could also get away with pre- and appending whitespaces to your strings:

def string_found(string1, string2):
    string1 = " " + string1.strip() + " "
    string2 = " " + string2.strip() + " "
    return string2.find(string1)
Answer from Felix Kling on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python-extract-words-from-given-string
Python | Extract words from given string - GeeksforGeeks
July 25, 2023 - The original string is : GeeksForGeeks is best Computer Science Portal The list of words is : ['GeeksForGeeks', 'is', 'best', 'Computer', 'Science', 'Portal'] ... In Python, using the find() function, we can extract string words.
Discussions

Python - Find words in string - Stack Overflow
I know that I can find a word in a string with if word in my_string: But I want to find all "word" in the string, like this. counter = 0 while True: if word in my_string: counter += 1... More on stackoverflow.com
🌐 stackoverflow.com
December 17, 2015
Finding a word within a string and then getting the "outter" characters until a space
here is one way: next((word for word in data.split(" ") if "awesome" in word), None) this will return the first word, or None if not found. you can also return all of matches: [word for word in data.split(" ") if "awesome" in word] More on reddit.com
🌐 r/pythontips
5
5
July 31, 2023
How to search for exact whole word match
Try re.sub() import re s = 'red redder red sreds red' s = re.sub(r'\bred\b', 'green', s) print(s) # green redder green sreds green \b means a word boundary if I can say so: https://regex101.com/r/aThn9o/1 More on reddit.com
🌐 r/learnpython
3
2
March 18, 2022
How to check for an exact word in a string?
Couple options IMO: def f(sentence, word): return word.lower() in sentence.lower().split(' ') This one is naive and will need enhancing to check for words with punctuation immediately after them etc. def f(sentence, word): import re rex = re.compile(f"\\b{word.lower()}\\b") return len(rex.findall(sentence.lower())) > 0 This one is more precise and uses regex word boundaries for you, but exposes you to all of the pitfalls regex brings to the table etc. More on reddit.com
🌐 r/learnpython
6
1
October 7, 2019
🌐
Stack Overflow
stackoverflow.com › questions › 48376460 › finding-just-full-words-in-a-python-string
finding just full words in a python string - Stack Overflow
yes the sql is relevant because I think it brings me objects not words, that is why I had to use the .__str__() to make a string representation of that object that could then be workable when searching for words in a phrase ... Save this answer. Show activity on this post. I dont't see why split() should not work. The issue is the .__str__() (which I don't see any need for). It creates one single string in which the keywords are searched - and then it will find substrings as well.
🌐
W3Schools
w3schools.com › python › ref_string_find.asp
Python String find() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The find() method finds the first occurrence of the specified value.
🌐
Finxter
blog.finxter.com › home › learn python blog › how to match an exact word in python regex? (answer: don’t)
How to Match an Exact Word in Python Regex? (Answer: Don't) - Be on the Right Side of Change
May 31, 2022 - However, a simpler and more Pythonic approach would be using the in keyword within membership expression 'hello' in 'hello world'. For a full match, use the start and end symbols '^hello$' that would not match the string 'hello world' but it would match 'hello'.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › how to search for exact whole word match
r/learnpython on Reddit: How to search for exact whole word match
March 18, 2022 -

I have a simple python script that parses a text file and replaces certain text words with alternates. The problem is that I need an exact word match. For example if I replace all text of "red" with a substitution, say "green" then all occurrences of red will be replaced with green, but an occurrence of "redrum" will be replace with "greenrum" and that's not what I want, because all text is significant and I will replace "greenrum" with something else. Note that the script will work if the occurrences of greenrum come before reddrum, but I have no way to tell what order the text is in. The substitution method I use is s.replace()...

s = s.replace('red', 'green')
s = s.replace('red2', 'green2')

I just need the first parameter to find exact whole word matches only.

🌐
Reddit
reddit.com › r/learnpython › how to check for an exact word in a string?
r/learnpython on Reddit: How to check for an exact word in a string?
October 7, 2019 -

I want to define a function f(sentence,word) which given some sentence 'x' will return true iff that sentence contains exactly the string 'y'.

For example, 'This is a sentence' and 'sentence', should return true but 'This is a sentence' and 'sent' should return false.

So currently I have:

if word in sentence:

return True

else: return false

Problem with this obviously it will return true even if the word appears within another word. How should I check for an exact word?

🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › (help with python 3) find whole words in text write function find_word (word, text) which finds occurrences of words in text. you can use str.find, but it will also find strings that are part of a word, e.g. "ape" in "shape". the function must be so-called "case insensitive", ie it must not distinguish between lowercase and uppercase letters. tips &
Solved (help with python 3) Find whole words in text Write | Chegg.com
November 2, 2021 - You can use str.find, but it will also find strings that are part of a word, e.g. "ape" in "shape". The function must be so-called "case insensitive", ie it must not distinguish between lowercase and uppercase letters. Tips & Warnings Convert everything to lowercase ("lower") Here’s the best way to solve it. ... This AI-generated tip is based on Chegg's full ...
🌐
Medium
medium.com › quantrium-tech › extracting-words-from-a-string-in-python-using-regex-dac4b385c1b8
Extracting Words from a string in Python using RegEx
October 6, 2020 - Here, findall is a method in re that takes two parameters — first the pattern to be searched, in this case it is 'Chennai' and second parameter is the content in string, from which it will search for the pattern.
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.7 ...
Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...
🌐
Quora
quora.com › How-can-I-do-a-full-string-match-in-Python
How to do a full string match in Python - Quora
Answer (1 of 6): Something like below (typed in interactive Python) [code]>>> domain = "www.example.com" >>> wordlist = ['example', 'pvt','ltd','examp'] >>> [word in domain for word in wordlist] [True, False, False, True] [/code]It says if the word from wordlist appears in the string domain (and...
🌐
Reddit
reddit.com › r/learnpython › finding known word in a scrambled string
r/learnpython on Reddit: Finding known word in a scrambled string
August 17, 2021 -

I'm trying to find known words inside a string of scrambled letters. For example "reuonnoinfe" is "onefournine" scrambled. I have a method that is meant to breakdown the string as well as the spelling of the numbers, and look through the string for the characters that spell the word, if all are found, it returns true. But I'm trying to do this using the find() method but with no luck. I'm mostly trying rubber duck debugging here but also might just not be seeing something

word = "reuonnoinfe"
def find_spelling(arr) -> bool: # arr is chars spelling the number
    found = True
    for char in arr:
        if word.find(char) :
            print(char)
        else:
            found = False
    return found

🌐
Python Basics
pythonbasics.org › home › python basics › string find() in python
String find() in Python - pythonbasics.org
The find(query) method is built-in to standard python. Just call the method on the string object to search for a string, like so: obj.find("search"). The find(
🌐
Mimo
mimo.org › glossary › python › string-find
Python string.find(): Syntax, Usage, and Examples
# Find the position of the word "fox" position = sentence.find("fox") print(f"'fox' was found at index: {position}") # Outputs: 'fox' was found at index: 16 # Search for a word that doesn't exist not_found = sentence.find("cat") print(f"Index when 'cat' is not found: {not_found}") # Outputs: Index when 'cat' is not found: -1 # Use in a conditional statement if sentence.find("lazy") != -1: print("The word 'lazy' is in the sentence.") # Outputs: The word 'lazy' is in the sentence. Because .find() returns -1 on failure instead of raising an error, it is a safe way to check for substrings. ... If you don’t specify start or end, Python searches the entire string.