编写一下程序 从键盘上输入一个字符串,统计字符串中英文字母的个数及数字的个数。

1个回答

写回答

杨娟178

2025-12-17 16:23

+ 关注

Python
Python

您好,以下是您所需要的程序代码:Pythonnum_chars = 0num_words = 0num_special_chars = 0num_uppercase = 0num_lowercase = 0num_digits = 0num_punctuation = 0

input_str = input("请输入一个字符串:")

for char in input_str: if char.isalpha(): if char.isupper(): num_uppercase += 1 else: num_lowercase += 1 elif char.isdigit(): num_digits += 1 elif char in string.punctuation: num_punctuation += 1 else: num_special_chars += 1

word
word

print("英文字母个数:" , num_uppercase+num_lowercase)print("数字个数:" , num_digits)print("特殊字符个数:" , num_special_chars)print("大写字母个数:" , num_uppercase)print("小写字母个数:" , num_lowercase)print("标点符号个数:" , num_punctuation)
这个程序会从键盘上读取一个字符串,并对字符串中的字母、数字、特殊字符、大写字母和小写字母进行计数。程序最后会输出这些计数结果。希望对您有所帮助。

举报有用(0分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号