vowels = 'aeiou'
# take input from the user
ip_str = raw_input("Enter a string: ")
# make it suitable for caseless comparisions
ip_str = ip_str.casefold()
# make a dictionary with each vowel a key and value 0
count = {}.fromkeys(vowels,0)
# count the vowels
for char in ip_str:
if char in count:
count[char] += 1
print(count)
错误:
Line - ip_str = ip_str.casefold()
AttributeError: 'str' object has no attribute 'casefold'
请您参考如下方法:
Python 2.6 不支持 str.casefold() 方法。
来自str.casefold() documentation :
New in version 3.3.
您需要切换到 Python 3.3 或更高版本才能使用它。
除了自己实现 Unicode 大小写折叠算法之外,没有其他好的选择。参见 How do I case fold a string in Python 2?
但是,由于您在这里处理的是 bytestring(而不是 Unicode),您可以只使用 str.lower() 并完成它。
