在Python中从字符串中删除辅音 [英] Deleting consonants from a string in Python

查看:67
本文介绍了在Python中从字符串中删除辅音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我不确定是否需要计数器才能正常工作.答案应该是'iiii'.

Here is my code. I'm not exactly sure if I need a counter for this to work. The answer should be 'iiii'.

def eliminate_consonants(x):
        vowels= ['a','e','i','o','u']
        vowels_found = 0
        for char in x:
            if char == vowels:
                print(char)

eliminate_consonants('mississippi')

推荐答案

更正您的代码

if char == vowels:行是错误的.它必须是if char in vowels:.这是因为您需要检查在元音列表中是否存在该特定字符.除此之外,您还需要print(char,end = '')(在python3中)将输出全部打印为iiii.

Correcting your code

The line if char == vowels: is wrong. It has to be if char in vowels:. This is because you need to check if that particular character is present in the list of vowels. Apart from that you need to print(char,end = '') (in python3) to print the output as iiii all in one line.

最终程序将是

def eliminate_consonants(x):
        vowels= ['a','e','i','o','u']
        for char in x:
            if char in vowels:
                print(char,end = "")

eliminate_consonants('mississippi')

输出将是

iiii


其他方式包括

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆