题解 | #统计字符#

统计字符

https://www.nowcoder.com/practice/539054b4c33b4776bc350155f7abd8f5

s = input()
letter_count = 0
number_count = 0
blank_count = 0
other_count = 0

for char in s:
    # isalpha函数判断是否字母
    if char.isalpha():
        letter_count += 1
    # isnumeric函数判断是否数字
    elif char.isdigit():
        number_count += 1
    # isspace函数判断是否空格
    elif char.isspace():
        blank_count += 1
    # 其他情况继续+1
    else:
        other_count += 1
print(letter_count)
print(blank_count)
print(number_count)
print(other_count)

考察字符串的常用内置方法:
str.isalpha()判断元素是不是字母

str.isdigit()判断元素是不是数字

str.isspace() 判断元素是不是空格
#数据仓库与数据分析实习#
OD专栏练习梳理 文章被收录于专栏

OD专栏练习梳理

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务