-3

I have referred to some posts related to this on Stack Overflow. However I did not really find a very convincing way of doing this.

How would I have a function to return True or False depending on whether a word (which is input to the function) contains 0 or 1(or more) stars.

I tried something like this:

def ANY_CHAR_IS_star(word):
    return bool(re.match(r"^[*]?", word))

However this is return true for false cases as well. Not sure where am going wrong. A little weak on regex honestly

Mohammad Ansari
  • 1,076
  • 1
  • 11
  • 22
asimo
  • 2,340
  • 11
  • 29

3 Answers3

6

The most pythonic way of doing this would be

return '*' in word
Matan Shahar
  • 3,190
  • 2
  • 20
  • 45
0

The regex you are looking for is probably this one r".*[*].*"

As Matan already mentionned, people usually use the in keyword to verify if a string is a substring of another one.

BlueSheepToken
  • 5,751
  • 3
  • 17
  • 42
0

if you want to check the a character in string and count the occurence of that character satisfy a condition then here is a code ,

from collections import defaultdict

def defv():
    return 0

def func(string):
    dic = defaultdict(defv)
    for i in string :
        dic[i]+=1
    return dic

word = 'abcdef**abs**'
data = func(word)

# check *  is in word and haveing count say x then
check='*' # checking caracter
count=2
try :
    if data[check]>count:
        print('True',data[check]) # printing the number of occurence of that charater
    else:
        print('False')
except KeyError:
    print('character is not in the string.')
sahasrara62
  • 10,069
  • 3
  • 29
  • 44