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

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的海寧網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
編寫一個 Python 程序,使用 For 循環(huán)、while 循環(huán)和函數(shù)對字符串中的字母、數(shù)字和特殊字符進(jìn)行計數(shù)。
使用 For 循環(huán)計算字符串中字母數(shù)字和特殊字符的 Python 程序
這個 python 程序允許用戶輸入一個字符串。
首先,我們使用 For Loop 來迭代一個字符串中的字符。在 For 循環(huán)中,我們使用 Elif 語句
- 第一個語句中的 isalpha() 是檢查字符是否是字母表。如果為真,字母值遞增。
- 第二條語句中的 isdigit() 檢查字符是否為 digit。如果為真,數(shù)字值遞增。
- 否則,特殊字符值遞增。
# Python program to Count Alphabets Digits and Special Characters in a String
string = input("Please Enter your Own String : ")
alphabets = digits = special = 0
for i in range(len(string)):
if(string[i].isalpha()):
alphabets = alphabets + 1
elif(string[i].isdigit()):
digits = digits + 1
else:
special = special + 1
print("\nTotal Number of Alphabets in this String : ", alphabets)
print("Total Number of Digits in this String : ", digits)
print("Total Number of Special Characters in this String : ", special)Python 統(tǒng)計字符串輸出中的字母、數(shù)字和特殊字符
Please Enter your Own String : [[email protected]](/cdn-cgi/l/email-protection) 12 cd 1212
Total Number of Alphabets in this String : 5
Total Number of Digits in this String : 6
Total Number of Special Characters in this String : 5Python 程序使用 While 循環(huán)計數(shù)字母數(shù)字和特殊字符
在這個 Python 計數(shù)字母、數(shù)字和特殊字符的程序中,我們將每個字符與 A、A、Z、Z、0 和 9 進(jìn)行比較。根據(jù)結(jié)果,我們增加相應(yīng)的值。
# Python Program to Count Alphabets Digits and Special Characters in a String
str1 = input("Please Enter your Own String : ")
alphabets = digits = special = 0
for i in range(len(str1)):
if((str1[i] >= 'a' and str1[i] <= 'z') or (str1[i] >= 'A' and str1[i] <= 'Z')):
alphabets = alphabets + 1
elif(str1[i] >= '0' and str1[i] <= '9'):
digits = digits + 1
else:
special = special + 1
print("\nTotal Number of Alphabets in this String : ", alphabets)
print("Total Number of Digits in this String : ", digits)
print("Total Number of Special Characters in this String : ", special)
使用函數(shù)計算字符串中字母數(shù)字和特殊字符的程序
在這個程序中,我們將每個字符與 ASCII 值進(jìn)行比較,找出這個字符串中的字母、數(shù)字和特殊字符。
# Python Program to Count Alphabets Digits and Special Characters in a String
str1 = input("Please Enter your Own String : ")
alphabets = digits = special = 0
for i in range(len(str1)):
if(ord(str1[i]) >= 48 and ord(str1[i]) <= 57):
digits = digits + 1
elif((ord(str1[i]) >= 65 and ord(str1[i]) <= 90) or (ord(str1[i]) >= 97 and ord(str1[i]) <= 122)):
alphabets = alphabets + 1
else:
special = special + 1
print("\nTotal Number of Alphabets in this String : ", alphabets)
print("Total Number of Digits in this String : ", digits)
print("Total Number of Special Characters in this String : ", special)Python 統(tǒng)計字符串輸出中的字母、數(shù)字和特殊字符
Please Enter your Own String : abcd*()[[email protected]](/cdn-cgi/l/email-protection)
Total Number of Alphabets in this String : 7
Total Number of Digits in this String : 6
Total Number of Special Characters in this String : 9 當(dāng)前題目:Python程序:統(tǒng)計字符串中字母數(shù)字和特殊字符
文章起源:http://m.fisionsoft.com.cn/article/dpejcip.html


咨詢
建站咨詢
