新聞中心
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司,提供網(wǎng)站制作、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,是專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
編寫一個 Python 程序,用 For 循環(huán)和 ASCII 值計算字符串中的元音,并給出一個實(shí)例。
計算字符串中元音的 Python 程序示例 1
這個 python 程序允許用戶輸入一個字符串。接下來,它使用 For 循環(huán)計算該字符串中元音的總數(shù)。
這里,我們使用 Python For 循環(huán)來迭代字符串中的每個字符。在 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 統(tǒng)計字符串輸出中的元音
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
計算字符串中元音總數(shù)的程序示例 3
這個 python 程序使用 ASCII 值來統(tǒng)計元音。建議大家參考 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 統(tǒng)計字符串中的元音輸出
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 當(dāng)前名稱:Python程序:統(tǒng)計字符串中元音
路徑分享:http://m.fisionsoft.com.cn/article/dhohjsg.html


咨詢
建站咨詢
