Does Python have a string 'contains' substring method?
Use the in
operator:
if "blah" not in somestring:
continue
Does Python have a string contains how many substring method, allowing for overlap?
Strings have a method count
You can do
s = '12212'
s.count('12') # this equals 2
Edited for the changing question, the answer below was posted as a comment by tobias_k
To count with overlap,
count_all = lambda string, sub: sum(string[i:i+len(sub)] == sub for i in range(len(string) - len(sub) + 1))
This can be called with,
count_all('1111', '11') # this returns 3
How to see if a string ONLY contains a substring in python
No regex necessary. Relying on the fact that str.count
counts non-overlapping occurrences
len(target) * data.count(target) == len(data)
Simple string methods are 400-800% faster than regex here:
>>> import re
>>> target = "World"
>>> data = "World" * 3
>>> pattern = f"^({re.escape(target)})+$"
>>> %timeit len(target) * data.count(target) == len(data)
115 ns ± 0.352 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
>>> %timeit re.match(pattern, data) is not None
456 ns ± 2.88 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
>>> %timeit bool(data.replace(target, '')) # str.replace is faster again
51.7 ns ± 0.269 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
Check if a list of string contains particular letter or charcater in it(python)
lst = ['Mangos*','apples','REd']
for l in lst:
if 'M' in l or '*' in l:
print(l)
How do I check if a given Python string is a substring of another one?
Try using in
like this:
>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
Get string from list if that string contains substring
Use next(..)
with a generator. This will sort of break
the moment it finds the first instance. Of course, this is assuming one element definitely exists in your list.
def f(l, sub):
return next(x for x in l if sub in x)
How to check if string includes substring with an unknown character in Python?
re.findall
returns a list of all matches of a regex (docs.python.org/3/howto/regex.html) pattern in a string. The regex character .
matches any character, but not a newline.
import re
txt = "foobar"
x = re.findall("oo.ar", txt) # -> ['oobar']
y = re.findall("ooba.", txt) # -> ['oobar']
print(x)
print(y)
Determining if a string contains a word
words = 'blue yellow'
if 'blue' in words:
print 'yes'
else:
print 'no'
Edit
I just realized that nightly blues
would contain blue
, but not as a whole word. If this is not what you want, split the wordlist:
if 'blue' in words.split():
…
Check if a string contains substring or part of it
A more common task would be to find the longest common subsequence, but if you really are trying to do what you say in your question:
check if a string contains a substring or part of this substring.
Consider that this reduces to the following: At least one character must be in both strings.
This is trivial if you convert to a set and find the intersection:
>>> string = "Hello 13 World"
>>> substring = "01234567890"
>>> bool(set(string) & set(substring))
True
>>> string = 'Hello 13 world'
>>> substring = 'abc'
>>> bool(set(string) & set(substring))
False
Related Topics
Integer Division by Negative Number
Why Do Some Regex Engines Match .* Twice in a Single Input String
Regexp Finding Longest Common Prefix of Two Strings
How to Make Good Reproducible Pandas Examples
Call to Operating System to Open Url
Can Python Select What Network Adapter When Opening a Socket
Is There a Python Equivalent to Java'S Awt Robot Class
How to Use Export With Python on Linux
After Anaconda Installation, Conda Command Fails With "Importerror: No Module Named Conda.Cli"
Force Another Program'S Standard Output to Be Unbuffered Using Python
Store Large Data or a Service Connection Per Flask Session