任意输入一字符串,判断字符串元音字母(a、e、i、o、u)分别出现的个数,同时输
时间: 2023-11-09 15:02:38 浏览: 101
输入一个字符串,然后通过遍历字符串的每个字符,判断是否为元音字母。如果是元音字母,则对应的元音字母个数加一。最后输出每个元音字母出现的个数。
以下是对应的代码实现:
```python
def count_vowels(string):
vowels = {'a', 'e', 'i', 'o', 'u'}
vowel_count = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
for char in string:
if char.lower() in vowels:
vowel_count[char.lower()] += 1
for vowel, count in vowel_count.items():
print(f"字母 {vowel} 出现了 {count} 次")
input_string = input("请输入一个字符串:")
count_vowels(input_string)
```
运行代码后,会要求用户输入一个字符串。然后代码会根据用户输入的字符串计算每个元音字母(a、e、i、o、u)出现的次数,并输出结果。
例如,用户输入字符串"Hello World",代码会输出以下结果:
```
字母 a 出现了 0 次
字母 e 出现了 1 次
字母 i 出现了 0 次
字母 o 出现了 2 次
字母 u 出现了 0 次
```
相关问题
任意输入一字符串,判断字符串元音字母(A、E、I、O、U)分别出现的个数,同时输出删除元音字母后的字符串。
```python
s = input("请输入一个字符串:")
vowels = "AEIOUaeiou"
counts = [0] * 5 # 分别对应元音字母A、E、I、O、U的计数器
new_s = ""
for c in s:
if c in vowels:
index = vowels.index(c.upper())
counts[index] += 1
else:
new_s += c
print("元音字母出现次数:")
for i in range(5):
print(vowels[i], ":", counts[i])
print("删除元音字母后的字符串:")
print(new_s)
```
示例输入1:
```
Hello World!
```
输出1:
```
元音字母出现次数:
A : 0
E : 1
I : 0
O : 2
U : 0
删除元音字母后的字符串:
Hll Wrld!
```
示例输入2:
```
Python is awesome!
```
输出2:
```
元音字母出现次数:
A : 1
E : 2
I : 1
O : 3
U : 0
删除元音字母后的字符串:
Pythn s wsm!
```
用Python任意输入一字符串,判断字符串元音字母(A、E、I、O、U)分别出现的个数,同时输出删除元音字母后的字符串。
str = input("请输入一个字符串:")
vowels = ['A', 'E', 'I', 'O', 'U']
vowel_count = {'A':0, 'E':0, 'I':0, 'O':0, 'U':0}
new_str = ''
for i in str:
if i.upper() in vowels:
vowel_count[i.upper()] += 1
else:
new_str += i
print("元音字母出现个数为:")
for vowel in vowel_count:
print(vowel, vowel_count[vowel])
print("删除元音字母后的字符串为:", new_str)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)