Python程序:统计字符串中元音

创新互联Python教程:

创新互联建站网站建设公司,提供网站制作、成都网站制作,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!

编写一个 Python 程序,用 For 循环和 ASCII 值计算字符串中的元音,并给出一个实例。

计算字符串中元音的 Python 程序示例 1

这个 python 程序允许用户输入一个字符串。接下来,它使用 For 循环计算该字符串中元音的总数。

这里,我们使用 Python For 循环来迭代字符串中的每个字符。在 For Loop 中,我们使用 If 语句检查字符是否为 A、E、I、O、u、A、E、I、O、u,如果为真,则增加元音值,否则跳过该字符

提示:请参考弦文章了解蟒弦的一切。

# Python Program to Count Vowels in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0

for i in str1:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u' or i == 'A'
       or i == 'E' or i == 'I' or i == 'O' or i == 'U'):
        vowels = vowels + 1

print("Total Number of Vowels in this String = ", vowels)

计算元音的 Python 程序示例 2

在这个程序中,我们使用降低功能将字符串覆盖为小写。这样,您只能在 If 语句中使用 a、e、I、o、u(避免大写字母)。

# Python Program to Count Vowels in a String

str1 = input("Please Enter Your Own String : ")

vowels = 0
str1.lower()

for i in str1:
    if(i == 'a' or i == 'e' or i == 'i' or i == 'o' or i == 'u'):
        vowels = vowels + 1

print("Total Number of Vowels in this String = ", vowels)

Python 统计字符串输出中的元音

Please Enter Your Own String : Hello World
Total Number of Vowels in this String =  3
>>> 
Please Enter Your Own String : Tutorial Gateway
Total Number of Vowels in this String =  7

计算字符串中元音总数的程序示例 3

这个 python 程序使用 ASCII 值来统计元音。建议大家参考 ASCII 表文章了解 ASCII 值。

# Python Program to Count Vowels in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0

for i in str1:
    if(ord(i) == 65 or ord(i) == 69 or ord(i) == 73
       or ord(i) == 79 or ord(i) == 85
       or ord(i) == 97 or ord(i) == 101 or ord(i) == 105
       or ord(i) == 111 or ord(i) == 117):
        vowels = vowels + 1

print("Total Number of Vowels in this String = ", vowels)

Python 统计字符串中的元音输出

Please Enter Your Own String : Python Tutorial
Total Number of Vowels in this String =  5
>>> 
Please Enter Your Own String : Tutorial Gateway
Total Number of Vowels in this String =  7

网站名称:Python程序:统计字符串中元音
标题URL:http://www.csdahua.cn/qtweb/news14/109514.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网