李成笔记网

专注域名、站长SEO知识分享与实战技巧

Python入门100题之010:统计字母数字

题目:

输入一行字符串,统计字母和数字。

视频讲解:

Python入门100题之010:统计字母数字

代码1:

line = input('输入一行字符串:')
DIGITS, LETTERS = 0, 0

for c in line:
    if c.isdigit():
        DIGITS += 1
    elif c.isalpha():
        LETTERS += 1
    else:
        pass

print("LETTERS", LETTERS)
print("DIGITS", DIGITS)

代码2:

s = input('输入一行字符串:')
stat = {"DIGITS": 0, "LETTERS": 0}

for c in s:
    if c.isdigit():
        stat['DIGITS'] += 1
    elif c.isalpha():
        stat['LETTERS'] += 1
    else:
        pass

print(stat)

代码3:

line = input('输入一行字符串:')

LETTERS, DIGITS = 0, 0

for i in line:
    if ('a' <= i <= 'z') or ('A' <= i <= 'Z'):
        LETTERS += 1
    if '0' <= i <= '9':
        DIGITS += 1

print("LETTERS", LETTERS)
print("DIGITS", DIGITS)

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言