Python程序:统计字符串中字母数字和特殊字符

创新互联python教程:

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的海宁网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

编写一个 Python 程序,使用 For 循环、while 循环和函数对字符串中的字母、数字和特殊字符进行计数。

使用 For 循环计算字符串中字母数字和特殊字符的 Python 程序

这个 python 程序允许用户输入一个字符串。

首先,我们使用 For Loop 来迭代一个字符串中的字符。在 For 循环中,我们使用 Elif 语句

  • 第一个语句中的 isalpha() 是检查字符是否是字母表。如果为真,字母值递增。
  • 第二条语句中的 isdigit() 检查字符是否为 digit。如果为真,数字值递增。
  • 否则,特殊字符值递增。
# 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 统计字符串输出中的字母、数字和特殊字符

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 循环计数字母数字和特殊字符

在这个 Python 计数字母、数字和特殊字符的程序中,我们将每个字符与 A、A、Z、Z、0 和 9 进行比较。根据结果,我们增加相应的值。

# 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)

使用函数计算字符串中字母数字和特殊字符的程序

在这个程序中,我们将每个字符与 ASCII 值进行比较,找出这个字符串中的字母、数字和特殊字符。

# 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 统计字符串输出中的字母、数字和特殊字符

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

新闻标题:Python程序:统计字符串中字母数字和特殊字符
文章URL:http://www.shufengxianlan.com/qtweb/news29/546929.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联