Use the in operator:
if "blah" not in somestring:
continue
Note: This is case-sensitive.
Answer from Michael Mrozek on Stack OverflowUse the in operator:
if "blah" not in somestring:
continue
Note: This is case-sensitive.
You can use str.find:
s = "This be a string"
if s.find("is") == -1:
print("Not found")
else:
print("Found")
The
find()method should be used only if you need to know the position of sub. To check if sub is a substring or not, use theinoperator. (c) Python reference
Is there a way to check if a string is a number?
is string.find(substring) time complexity of O(n) or (O*m)?
Why is there no "dotted dict"?
How to check if a line in Bash has a newline
A trailing newline specifically? Use:
[[ $line == *$'\n' ]]
A newline anywhere in the string? Use:
[[ $line == *$'\n'* ]]
$'...' is a form of quoting in Bash that understands C-style escapes.
I know there is the in keyword which does the job and does it well, but it's a question of consistency. Isn't one of those python zens all about consistency?
When I can do str.startswith('Foo') and also str.endswith('Foo'), then str.contains('Foo') should be quite obvious and intuitive isn't it? Instead of that, I have to do this:
if 'Foo' in str:
do_something()
While this does the job and does it great, the contains() method is more practical and intuitive, isn't it? And it's not that the core team even has to do a lot of effort for that. We already have str.__contains__('Foo') which works, so all they have to do is to turn it into a proper method!