python中如何对字符串计数_对字符串Python中的特定字符进行计数

试图找出Python中允许用户输入一个句子的最佳方法,然后计算该句子中的字符数,以及计算元音的数量。我希望输出返回字符总数,加上A的总数,O的总数,U的总数等等。下面是到目前为止我得到的代码:# prompt for input

sentence = input('Enter a sentence: ')

# count of the number of a/A occurrences in the sentence

a_count = 0

# count of the number of e/E occurrences in the sentence

e_count = 0

# count of the number of i/I occurrences in the sentence

i_count = 0

# count of the number of o/O occurrences in the sentence

o_count = 0

# count of the number of u/U occurrences in the sentence

u_count = 0

# determine the vowel counts and total character count

length=len(sentence)

if "A" or "a" in sentence :

a_count = a_count + 1

if "E" or "e" in sentence :

e_count = e_count + 1

if "I" or "i" in sentence :

i_count = i_count + 1

if "O" or "o" in sentence :

o_count = o_count + 1

if "U" or "u" in sentence :

u_count = u_count + 1

#Display total number of characters in sentence

print("The sentence", sentence, "has", length,"characters, and they are\n",

a_count, " a's\n",

e_count, "e's\n",

i_count, "i's\n",

o_count, "o's\n",

u_count, "u's")

问题是,当我运行这个程序时,我只为每个元音获取一个字符,这意味着我的代码实际上并没有按照我想要的方式计算单个元音。任何人输入如何修复这个基于我已经提出的代码将不胜感激

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值