Assuming your string is s:
'$' in s # found
'$' not in s # not found
# original answer given, but less Pythonic than the above...
s.find('$')==-1 # not found
s.find('$')!=-1 # found
And so on for other characters.
... or
pattern = re.compile(r'[\d\$,]')
if pattern.findall(s):
print('Found')
else:
print('Not found')
... or
chars = set('0123456789$,')
if any((c in chars) for c in s):
print('Found')
else:
print('Not Found')
Answer from dappawit on Stack OverflowAssuming your string is s:
'$' in s # found
'$' not in s # not found
# original answer given, but less Pythonic than the above...
s.find('$')==-1 # not found
s.find('$')!=-1 # found
And so on for other characters.
... or
pattern = re.compile(r'[\d\$,]')
if pattern.findall(s):
print('Found')
else:
print('Not found')
... or
chars = set('0123456789$,')
if any((c in chars) for c in s):
print('Found')
else:
print('Not Found')
user Jochen Ritzel said this in a comment to an answer to this question from user dappawit. It should work:
('1' in var) and ('2' in var) and ('3' in var) ...
'1', '2', etc. should be replaced with the characters you are looking for.
See this page in the Python 2.7 documentation for some information on strings, including about using the in operator for substring tests.
Update: This does the same job as my above suggestion with less repetition:
# When looking for single characters, this checks for any of the characters...
# ...since strings are collections of characters
any(i in '<string>' for i in '123')
# any(i in 'a' for i in '123') -> False
# any(i in 'b3' for i in '123') -> True
# And when looking for subsrings
any(i in '<string>' for i in ('11','22','33'))
# any(i in 'hello' for i in ('18','36','613')) -> False
# any(i in '613 mitzvahs' for i in ('18','36','613')) ->True
Is there some easy, built-in way to check if a given string contains at least one single digit, 0-9? Everything I'm searching talks about the in operator or the find method, but those seem to require that you already know which digit you are looking for.
I'm leaning toward using an RE, but I wanted to know if there was a simpler way first.
Examples that would evaluate to true would be:
'44 a b' 'aa ba 5' '45 187'
and false would be any string without at least one digit.
I figure I can just try to match it with \d+, but I don't want to rely on REs too much, even though I find them fun!
Thanks!
I'm really new to python and i'm having some trouble figuring out how to check if a list contains a certain character.
For example:
list = ['a1', 'a2', 'b1', 'b2', 'a3', 'a5', 'c3']
how would I check to see what elements contain the string 'a'? sorry if i didn't word my question properly i'm still very new to coding, but any help would be appreciated.
Use 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
You could use any here.
>>> string = r"/\?%"
>>> test = "This is my string % my string ?"
>>> any(elem in test for elem in string)
True
>>> test2 = "Just a test string"
>>> any(elem in test2 for elem in string)
False
I think Sukrit probably gave the most pythonic answer. But you can also solve this with set operations:
>>> test_characters = frozenset(r"/\?%")
>>> test = "This is my string % my string ?"
>>> bool(set(test) & test_characters)
True
>>> test2 = "Just a test string"
>>> bool(set(test2) & test_characters)
False