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

写一个 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 and Consonants in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0
consonants = 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
    else:
        consonants = consonants + 1

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

Python 计算字符串输出中的元音和辅音

Please Enter Your Own String : Hello WOrld
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  8
>>> 
Please Enter Your Own String : Python Programs
Total Number of Vowels in this String =  3
Total Number of Consonants in this String =  12

计算字符串中元音和辅音的程序示例 2

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

# Python Program to Count Vowels and Consonants in a String

str1 = input("Please Enter Your Own String : ")
vowels = 0
consonants = 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
    else:
        consonants = consonants + 1

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

计算元音和辅音总数的 Python 程序示例 3

这个程序使用 ASCII 值来查找元音和辅音。建议大家参考 ASCII 表文章了解 ASCII 值。

# Python Program to Count Vowels and Consonants in a String

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

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
    elif((ord(i) >= 97 and ord(i) <= 122) or (ord(i) >= 65 and ord(i) <= 90)):
        consonants = consonants + 1

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

Python 计算字符串输出中的元音和辅音

Please Enter Your Own String : Python Examples
Total Number of Vowels in this String =  4
Total Number of Consonants in this String =  10
>>> 
Please Enter Your Own String : Learn Python Programming
Total Number of Vowels in this String =  6
Total Number of Consonants in this String =  16
>>> 

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 11:55
下一篇 2024-12-12 11:55

相关推荐

  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python中将字符串转化为浮点数

    本文将介绍在Python中将字符串转化为浮点数的常用方法。在介绍方法之前,我们先来思考一下这个问题应该如何解决。 一、eval函数 在Python中,最简单、最常用的将字符串转化为…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29
  • Python学习笔记:去除字符串最后一个字符的方法

    本文将从多个方面详细阐述如何通过Python去除字符串最后一个字符,包括使用切片、pop()、删除、替换等方法来实现。 一、字符串切片 在Python中,可以通过字符串切片的方式来…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29
  • 爬虫是一种程序

    爬虫是一种程序,用于自动获取互联网上的信息。本文将从如下多个方面对爬虫的意义、运行方式、应用场景和技术要点等进行详细的阐述。 一、爬虫的意义 1、获取信息:爬虫可以自动获取互联网上…

    编程 2025-04-29
  • Vb运行程序的三种方法

    VB是一种非常实用的编程工具,它可以被用于开发各种不同的应用程序,从简单的计算器到更复杂的商业软件。在VB中,有许多不同的方法可以运行程序,包括编译器、发布程序以及命令行。在本文中…

    编程 2025-04-29

发表回复

登录后才能评论