You can use the word-boundaries of regular expressions. Example:

import re

s = '98787This is correct'
for words in ['This is correct', 'This', 'is', 'correct']:
    if re.search(r'\b' + words + r'\b', s):
        print('{0} found'.format(words))

That yields:

is found
correct found

For an exact match, replace \b assertions with ^ and $ to restrict the match to the begin and end of line.

Answer from Birei on Stack Overflow
🌐
Anthropic
anthropic.com › engineering › demystifying-evals-for-ai-agents
Demystifying evals for AI agents
January 9, 2026 - Popular benchmarks like Terminal-Bench 2.0 ship through the Harbor registry, making it easy to run established benchmarks along with custom eval suites. Promptfoo is a lightweight, flexible, and open-source framework that focuses on declarative YAML configuration for prompt testing, with assertion types ranging from string matching to LLM-as-judge rubrics.
🌐
W3Schools
w3schools.com › python › ref_string_strip.asp
Python String strip() Method
Leading means at the beginning of the string, trailing means at the end. You can specify which character(s) to remove, if not, any whitespaces will be removed. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Discussions

How to use str.contains to get exact matches and not partial ones?
If you're looking for exact matches, str.contains may not be the function you should be using. The output looks correct to me in that all of the strings in the output do contain your keyword. More on reddit.com
🌐 r/learnpython
11
2
November 10, 2021
Match non-exact strings? Is it possible?
You may want to look into Regular Expressions to clean them up. In SPSS there's no native Regular Expressions support (at least, that's what a google search told me) but you can use Python code in SPSS somehow to do it as noted here . Luckily, this also allows you to look at this python regex tutorial which I liked a lot. More on reddit.com
🌐 r/statistics
23
1
August 11, 2013
How do I find strings in a row that are an exact match using pandas str.match or str.contains
I got it. Man that was causing my code fits for a few strings :$ new_dataframe = df[df['number'] == number] More on reddit.com
🌐 r/learnpython
2
3
June 15, 2017
exact match using list comprehension
'abc_a' in my_list should work. Don't need a list comprehension here. More on reddit.com
🌐 r/learnpython
6
5
January 18, 2018
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › check-whether-two-strings-are-anagram-of-each-other
Check if two Strings are Anagrams of each other - GeeksforGeeks
Time Complexity: O(m × log(m) ... to an space used is O(m + n). The idea is to use a hash map or dictionary count the frequency of each character in both the input strings....
Published   July 25, 2025
🌐
Note.nkmk.me
note.nkmk.me › home › python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - This article explains string comparisons in Python, covering topics such as exact matches, partial matches, forward/backward matches, and more. Exact match (equality comparison): ==, != Partial match: ...
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
3 days ago - For example, a*a will match 'aaaa' because the a* will match all 4 'a's, but, when the final 'a' is encountered, the expression is backtracked so that in the end the a* ends up matching 3 'a's total, and the fourth 'a' is matched by the final 'a'. However, when a*+a is used to match 'aaaa', the a*+ will match all 4 'a', but when the final 'a' fails to find any more characters to match, the expression cannot be backtracked and will thus fail to match. x*+, x++ and x?+ are equivalent to (?>x*), (?>x+) and (?>x?) correspondingly. Added in version 3.11. ... Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match.
🌐
Python
docs.python.org › 3 › library › struct.html
struct — Interpret bytes as packed binary data
1 week ago - Return a bytes object containing the values v1, v2, … packed according to the format string format. The arguments must match the values required by the format exactly.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › enum.html
enum — Support for enumerations
1 week ago - The result of any string operation performed on or with a StrEnum member is not part of the enumeration. >>> from enum import StrEnum, auto >>> class Color(StrEnum): ... RED = 'r' ... GREEN = 'g' ... BLUE = 'b' ... UNKNOWN = auto() ... >>> Color.RED <Color.RED: 'r'> >>> Color.UNKNOWN <Color.UNKNOWN: 'unknown'> >>> str(Color.UNKNOWN) 'unknown' ... There are places in the stdlib that check for an exact str instead of a str subclass (i.e.
🌐
Reddit
reddit.com › r/learnpython › how to use str.contains to get exact matches and not partial ones?
r/learnpython on Reddit: How to use str.contains to get exact matches and not partial ones?
November 10, 2021 -

Hi, I don't get why when I use str.contains to get exact matches from a list of keywords, the output still contains partial matches. Here is an extract of what I have (I'm only including one keyword in the list for the example):

keyword= ['SE.TER.ENRL']

subset = df[df['Code'].str.contains('|'.join(keyword), case=False, na=False)]

Output: ['SE.TER.ENRL' 'SE.TER.ENRL.FE' 'SE.TER.ENRL.FE.ZS']

Does anyone know how to get around this?

Thanks!

🌐
Quora
quora.com › How-do-you-use-a-Python-regular-expression-to-find-an-exact-string-match
How to use a Python regular expression to find an exact string match - Quora
Answer (1 of 3): First of all, I should mention that Regular Expressions is extremely versatile. Whilst it can be used in this application, a normal search algorithm can do it fine. For this case; [code]paragraphs = re.findall(r' (.*?) ', str(respData)) # . = Any Character except fo...
🌐
Plain English Westminster
benhoyt.com › writings › python-pattern-matching
Structural pattern matching in Python 3.10
It also seems less obvious to me with the cases “backwards”, falling through to the looser matches. And [_, _] followed by [*_] to mean “not of length 2” is not exactly explicit. Warehouse, PyPI’s server code, has 59,000 lines of Python code, including tests.
🌐
Johns Hopkins University
cs.jhu.edu › ~langmea › resources › lecture_notes › 03_strings_exact_matching_v2.pdf pdf
Strings and Exact Matching Ben Langmead Department of Computer Science
Strings and Exact Matching · Ben Langmead · Department of Computer Science · Please sign guestbook (www.langmead-lab.org/teaching-materials) to tell me briefly how you are using the slides. For original Keynote · files, email me (ben.langmead@gmail.com).
🌐
Riot Games
developer.riotgames.com › apis
Riot Developer Portal
© 2021 Riot Games, Inc. All rights reserved. League of Legends and Riot Games are trademarks, service marks, and registered trademarks of Riot Games, Inc
🌐
Python
docs.python.org › 3 › library › index.html
The Python Standard Library — Python 3.14.3 documentation
While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It...
🌐
AskPython
askpython.com › home › matching entire strings in python using regular expressions
Matching Entire Strings in Python using Regular Expressions - AskPython
February 27, 2023 - The re.match method works like re.search, but only matches the pattern at the beginning of the string. To match an exact string, you can use the ^ and $ anchors to match the start and end of the string.
🌐
Quora
quora.com › How-do-you-match-an-exact-word-with-Regex-Python
How to match an exact word with Regex Python - Quora
Answer (1 of 10): You said “exact word”, and technically, the regex [code ]r'word'[/code] would get you that. However, I get the impression that might not be what you want, because “turn” and “turning” are different words, and [code ]r'turn'[/code] would get you both.
🌐
Django
docs.djangoproject.com › en › 6.0 › topics › db › queries
Making queries | Django documentation | Django
This is for convenience, because exact lookups are the common case. ... A case-insensitive match.
🌐
Wikipedia
en.wikipedia.org › wiki › Boyer–Moore_string-search_algorithm
Boyer–Moore string-search algorithm
3 weeks ago - If t′ does not exist, then shift the left end of P to the right by the least amount (past the left end of t in T) so that a prefix of the shifted pattern matches a suffix of t in T. This includes cases where t is an exact match of P.
🌐
DataCamp
datacamp.com › tutorial › fuzzy-string-python
Fuzzy String Matching in Python Tutorial | DataCamp
February 6, 2019 - Combining the edit operations together enables you to discover the list of possible strings that are N edits away, where N is the number of edit operations. For example, the edit distance between “London” and “Londin” is one since replacing the “i” with an “o” leads to an exact match.
🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
The result should be correctly rounded to a given precision p of digits after the decimal point. The rounding mode for float matches that of the round() builtin.