a =input("Write a sentence or phrase :")
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
print(key, a.count(key))
我试着计算每一封字母大写字母,小字母包含,如果我需要更清楚我怎么能理解大写字母,每个人都已经理解了我的意思。谢谢大家的答案。我已经解决了!
答案 0 :(得分:1)
尝试先从输入中删除所有内容:
initialInput = input("Write a sentence or phrase :")
lowerInput = initialInput.lower()
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
print(key, lowerInput.count(key))
答案 1 :(得分:0)
您可以在字符串输入上调用lower()
方法:
for key in symbol:
print (key, a.lower().count(key))
答案 2 :(得分:0)
在接受输入时只需使用a.lower()
,并在循环中计数。
a =input("Write a sentence or phrase :").lower()
symbol = 'abcdefghijklmnopqrstuvwxyz'
for key in symbol:
print(key, a.count(key))
或使用Counter
,然后您不需要symbol
进行比较。
from collections import Counter
a =input("Write a sentence or phrase :").lower()
print(Counter(a))
答案 3 :(得分:0)
就个人而言,我会使用Counter
from collections import Counter
counts = Counter("Hello, world!")
for k, v in counts.items():
print(k, v)