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

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

这个 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)

在这个程序中,我们使用降低功能将字符串覆盖为小写。这样,您只能在 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

这个 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

原创文章,作者:UGQVY,如若转载,请注明出处:https://www.506064.com/n/126665.html

相关推荐

  • Python 程序:查找字符串中的单词和字符数

    如何计算 python 字符串中的单词和字符? 在这个字符串 python 程序中,我们需要计算一个字符串中的字符和单词数。让我们检查一个例子“我爱我的国家”在这个字符串中,我们的…

    编程 2024-10-03
  • 包含python中动态特性的实现的词条

    本文目录一览: 1、如何系统地自学 Python 2、python的动态语言特性具体体现在哪?给点实例 3、如何实现 C/C++ 与 Python 的通信 4、python的特性是…

    编程 2025-01-07
  • 使用Python创建数据库表格

    现代信息技术快速发展,数据库已成为各种应用开发中不可或缺的基础设施之一。Python作为一种流行的编程语言,也提供了许多库和框架来帮助Python开发人员连接和操作数据库。在这篇文…

    编程 2024-10-03
  • python字节流转为数组,python 元组转数组

    本文目录一览: 1、Python中字符串与数组的转换方法是什么? 2、python中怎样将带空字符串的字符串数组转换成int数组 3、Python中如何将str格式的字符串转化成a…

    编程 2024-12-14
  • python多线程原理及其实现的简单介绍

    本文目录一览: 1、Python多线程总结 2、python 怎么实现多线程的 3、Python多线程是什么意思 4、python多线程几种方法实现 5、Python多线程是什么意…

    编程 2024-11-30
  • python入门系列之元组(python中的元组操作)

    本文目录一览: 1、python里面元组是什么意思? 2、Python中的元组(Tuple) 3、python基础之序列类型的方法——列表amp;元组 4、Python中的基本数据…

    编程 2024-10-03
  • 疯狂Python讲义的全面掌握与实践

    本文将从多个方面对疯狂Python讲义进行详细的阐述,帮助读者全面了解Python编程,掌握疯狂Python讲义的实现方法。 一、Python基础语法 Python基础语法是学习P…

    编程 2025-04-28
  • python-数字反转,python数字反转输出

    本文目录一览: 1、python怎么将数字反转后输出 2、reverse在python里是什么意思 3、把一个数字反过来输出,用python解 4、Python使用循环结构编写:输…

    编程 2025-01-04
  • 微信小程序复选框的实现方法

    微信小程序中的复选框是一种取值为 true 或 false 的控件,它可以展示多个选项,用户可以选择其中一个或多个选项。在本文中,我们将从多个方面介绍微信小程序复选框的实现方法和相…

    编程 2024-10-04
  • Python str.find函数详解

    在Python中,字符串是最常用的数据类型之一。而Python的str.find()函数是处理字符串中一个非常有用的工具,它可以帮助我们查找特定字符或子字符串的位置。在本文中,我们…

    编程 2025-01-09