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/zh-hant/n/235534.html

相關推薦

  • 如何修改mysql的端口號

    本文將介紹如何修改mysql的端口號,方便開發者根據實際需求配置對應端口號。 一、為什麼需要修改mysql端口號 默認情況下,mysql使用的端口號是3306。在某些情況下,我們需…

    編程 2025-04-29
  • 英語年齡用連字符號(Hyphenation for English Age)

    英語年齡通常使用連字符號表示,比如 “five-year-old boy”。本文將從多個方面探討英語年齡的連字符使用問題。 一、英語年齡的表達方式 英語中表…

    編程 2025-04-29
  • Idea新建文件夾沒有java class的解決方法

    如果你在Idea中新建了一個文件夾,卻沒有Java Class,應該如何解決呢?下面從多個方面來進行解答。 一、檢查Idea設置 首先,我們應該檢查Idea的設置是否正確。打開Id…

    編程 2025-04-29
  • 金額選擇性序列化

    本文將從多個方面對金額選擇性序列化進行詳細闡述,包括其定義、使用場景、實現方法等。 一、定義 金額選擇性序列化指根據傳入的金額值,選擇是否進行序列化,以達到減少數據傳輸的目的。在實…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29