You can use any:
a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]
if any(x in a_string for x in matches):
Similarly to check if all the strings from the list are found, use all instead of any.
You can use any:
a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]
if any(x in a_string for x in matches):
Similarly to check if all the strings from the list are found, use all instead of any.
any() is by far the best approach if all you want is True or False, but if you want to know specifically which string/strings match, you can use a couple things.
If you want the first match (with False as a default):
match = next((x for x in a if x in a_string), False)
If you want to get all matches (including duplicates):
matches = [x for x in a if x in a_string]
If you want to get all non-duplicate matches (disregarding order):
matches = {x for x in a if x in a_string}
If you want to get all non-duplicate matches in the right order:
matches = []
for x in a:
if x in a_string and x not in matches:
matches.append(x)
Hey I want to use regex to check if a string contains multiple of the same character.
example:
ymdrchgaau
contains the character "a" two times, instead of the a it could also be any other letter which I do not know before . How do I use regex to check for that?
I tried using [a-z]{2} but that only checks for every character not if one character has multiple appearances. The only other Idea I have is using something like this:
[a]{2}|[i]{2}|[f]{2}
for every character in the abc but that would be extrem long.
How to check a string for multiple letters in python - Stack Overflow
python - how to check multiple characters in a string more efficiently - Stack Overflow
python - How to check a string for specific characters? - Stack Overflow
How can you check if a string contains any one character from a set of characters?
a_string = "abcde"
letters = ["a", "e", "i", "o", "u"]
for x in a_string:
if(x in letters):
print(x+" is in "+a_string)
From here you can use a dictionary to map "x" to a point value.
This can easily be done using a dictionary and for loop.
# Dictionary matching letters to values
letter_val = {'a': 1,
'b': 2,
'c': 3,
'd': 4
}
def myFunction(s):
"""Function takes a string and checks each character
against the dictionary values. If letter is in then
add value to result"""
res = 0
for char in s:
if char in letter_val:
res += letter_val[char]
return res
The general solution to your problem is a solution space called "Pattern matching." This particular kind of pattern matching appears to be a regular expression.
Though you haven't specified what your pattern is supposed to be, given your input I will assume that a matching string will be something like "Three letters, followed by three numbers, repeated an arbitrary number of times." That regular expression is:
import re
pat = re.compile(r'''
(?: # open the group so we can repeat it later
\w{3} # three letters
\d{3} # three numbers
)+ # repeated one or more times
''', re.X)
You can then check a string against this pattern with re.match
s = 'aaa111bbb222ccc333ddd444eee555fff666'
assert re.match(pat, s)
s2 = 'arbitrary non-matching string'
assert not re.match(pat, s2)
Can this function help you??
str = "aaa111bbb222ccc333ddd444eee555fff666"
def my_is_alpha(string, list):
for start, end in list:
if not string[start:end].isalpha():
return False
return True
list = [(0, 3), (6, 9), (12, 15)]
print(my_is_alpha(str, list))
Output:
> True
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
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!
You can use any with generator expression
A = [...]
chars = ('0', '2', '4', '6', '8')
return any(c in A for c in chars)
You can try a for loop:
for i in '02468':
if i not in A:
return False
If you want True to be returned if all of the characters are found:
for i in '02468':
if i not in A:
return False
return True
Yes, you can get the solution in one line easily with the count method of a string:
>>> # I named it 'mystr' because it is a bad practice to name a variable 'str'
>>> # Doing so overrides the built-in
>>> mystr = "Hello! My name is Barney!"
>>> mystr.count("!")
2
>>> if mystr.count("!") == 2:
... print True
...
True
>>>
>>> # Just to explain further
>>> help(str.count)
Help on method_descriptor:
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
>>>
Use str.count method:
>>> s = "Hello! My name is Barney!"
>>> s.count('!')
2
BTW, don't use str as variable name. It shadows builtin str function.