my_string = "Mary had a little lamb"# simplest solution, using count, is case-sensitivemy_string.count("m") # yields 1import re# case-sensitive with regexlen(re.findall("m", my_string))# three ways to get case insensitivity - all yield 2len(re.findall("(?i)m", my_string))len(re.findall("m|M", my_string))len(re.findall(re.compile("m",re.IGNORECASE), my_string))
import re
def count(s, ch):
pass
def main():
s = raw_input ("Enter strings what you like, for example, 'welcome': ")
ch = raw_input ("Enter you want count characters, but best result to find one character: " )
print ( len (re.findall ( ch, s ) ) )
main()
# Objective: we will only count for non-empty characters
text = "count a character occurrence"unique_letters = set(text)result = dict((x, text.count(x)) for x in unique_letters if x.strip())
print(result)# {'a': 3, 'c': 6, 'e': 3, 'u': 2, 'n': 2, 't': 2, 'r': 3, 'h': 1, 'o': 2}
counts_dict = {}for c in list(sentence):if c not in counts_dict:counts_dict[c] = 0counts_dict[c] += 1
for key, value in counts_dict.items():print(key, value)
sentence1 =" Mary had a little lamb"count = {}for i in sentence1:if i in count:count[i.lower()] = count[i.lower()] + 1else:count[i.lower()] = 1print(count)