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
Issue when checking for special character in a string
How to check if a string contains a character in a list
How can you check if a string contains any one character from a set of characters?
Pandas - filter df rows where column contains str form another column
Videos
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.