求下题的Python解法

img


之前有课开小差,已经有点跟进度不上了,现在还有点懵,希望得到解答

自己写个函数测试下,我这里没有去调试,你可以用网页ide测试并根据提示修改


def count_chars(input_string):
    char_count = {}
    for char in input_string:
        if char.isalpha():
            char_count['letters'] = char_count.get('letters', 0) + 1
        elif char.isdigit():
            char_count['numbers'] = char_count.get('numbers', 0) + 1
        elif char.isspace():
            char_count['spaces'] = char_count.get('spaces', 0) + 1
        else:
            char_count['other'] = char_count.get('other', 0) + 1
    return char_count

input_string = input("请输入字符串:")
result = count_chars(input_string)
print(result)

循环遍历输入的字符串,然后逐个统计总字符,字母,数字,空格,其他字符的个数即可。

代码如下:

参考链接:


python 判断字符串中包含空格、数字、某个字符或直接为空_python请输入一个包含空格的字符串_打个酱油卖萌‍♂️的博客-CSDN博客 str='kj25d df58'if str==''print('该字符串为空')else:for i in str:if i==' ':print('该字符串包含空格')if i.isdigit():print('该字符串包含数字') https://blog.csdn.net/weixin_44123630/article/details/109904564


s=input("请输入字符串:")   # 从输入获取一个字符串 

chs=0      # 总字符数
letters=0  # 英文字母数
nums=0     # 数字数
spaces=0   # 空格数
others=0   # 其他字符数

for ch in s:

    chs+=1             # 总字符数+1
 # https://blog.csdn.net/Refrain__WG/article/details/89214660
    if ch.isalpha():   #判断英文字母
        letters+=1
    elif ch.isalnum(): # 判断数字
        nums+=1
    #  https://blog.csdn.net/weixin_44123630/article/details/109904564  
    elif ch==' ':      # 判断空格
        spaces+=1
    else:              # 其他字符
        others+=1

# 打印结果
print("所有字符的总数为:\t",chs)  # 根据题目的输出,推测这里应是输出字符总数
print("英文字母出现的次数为:\t",letters)
print("数字出现的次数为:\t",nums)
print("空格出现的次数:\t",spaces)
print("其他字符出现的次数:\t",others)

img



def strCount(input_str):
    result = {}
    for item in input_str:
        # 判断数字
        if item.isdigit():
            if 'number' not in result.keys():
                result['number'] = 1
            else:
                result['number'] = result.get('number') + 1
        # 判断英文字母
        elif item.isalpha():
            if 'letters' not in result.keys():
                result['letters'] = 1
            else:
                result['letters'] = result.get('letters') + 1
        # 判断空格
        elif item.isspace():
            if 'space' not in result.keys():
                result['space'] = 1
            else:
                result['space'] = result.get('space') + 1
        # 判断其他字符
        else:
            if 'other' not in result.keys():
                result['other'] = 1
            else:
                result['other'] = result.get('other') + 1


    return result


# input_str = "This is   a test. 123   45678!!! End?"
input_str = input("请输入字符串:")
result = strCount(input_str)

number_count = result.get('number')
letters_count = result.get('letters')
space_count = result.get('space')
other_count = result.get('other')
total_count = number_count + letters_count + space_count + other_count

print('所有字母的总数为:\t',  total_count  )
print('英文字母出现的次数:\t' , letters_count )
print('数字出现的次数:\t' , number_count)
print('空格出现的次数:\t' ,space_count )
print('其他字符出现的次数:\t' ,other_count )