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 Overflowpython - How to check a string for specific characters? - Stack Overflow
check whether a string contains a character in python - Stack Overflow
How can you check if a string contains any one character from a set of characters?
How to check if a string contains a character in a list
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')
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
Your problem is on this line:
holderString = 0
You reassigned the holderString variable to the integer 0. While strings can be iterated over, integers cannot. You attempt to iterate over the new integer on this line:
if s[i] not in holderString:
which causes the error.
There is a much better way to approach a function that returns the first repeated character though. Simply use the index() method:
def findChar(char, string):
for c in string:
if c == char:
return string.index(c)
It appears you only need to count unique characters until you find the first duplicate. You can do that with a set.
def longest_substring(s: str) -> int:
seen = set()
for c in s:
if c in seen:
break
seen.add(c)
return len(seen)
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!