日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Python程序:統(tǒng)計(jì)字符串中字母數(shù)字和特殊字符

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

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的海寧網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

編寫(xiě)一個(gè) Python 程序,使用 For 循環(huán)、while 循環(huán)和函數(shù)對(duì)字符串中的字母、數(shù)字和特殊字符進(jìn)行計(jì)數(shù)。

使用 For 循環(huán)計(jì)算字符串中字母數(shù)字和特殊字符的 Python 程序

這個(gè) python 程序允許用戶(hù)輸入一個(gè)字符串。

首先,我們使用 For Loop 來(lái)迭代一個(gè)字符串中的字符。在 For 循環(huán)中,我們使用 Elif 語(yǔ)句

  • 第一個(gè)語(yǔ)句中的 isalpha() 是檢查字符是否是字母表。如果為真,字母值遞增。
  • 第二條語(yǔ)句中的 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)計(jì)字符串輸出中的字母、數(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 :   5

Python 程序使用 While 循環(huán)計(jì)數(shù)字母數(shù)字和特殊字符

在這個(gè) Python 計(jì)數(shù)字母、數(shù)字和特殊字符的程序中,我們將每個(gè)字符與 A、A、Z、Z、0 和 9 進(jìn)行比較。根據(jù)結(jié)果,我們?cè)黾酉鄳?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ù)計(jì)算字符串中字母數(shù)字和特殊字符的程序

在這個(gè)程序中,我們將每個(gè)字符與 ASCII 值進(jìn)行比較,找出這個(gè)字符串中的字母、數(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)計(jì)字符串輸出中的字母、數(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

網(wǎng)頁(yè)題目:Python程序:統(tǒng)計(jì)字符串中字母數(shù)字和特殊字符
轉(zhuǎn)載來(lái)源:http://www.dlmjj.cn/article/dpejcip.html