用python编写一段程序:输入一句中文句子,请编程统计一句话中汉字(包含数字等)和标点符号的个数。
时间: 2024-05-22 15:16:30 浏览: 72
sentence = input("请输入一句中文句子:")
num_chinese = 0 # 汉字个数
num_punctuation = 0 # 标点符号个数
for char in sentence:
if char.isnumeric() or char.isalpha():
num_chinese += 1
elif char.isspace():
continue
else:
num_punctuation += 1
print("汉字个数为:", num_chinese)
print("标点符号个数为:", num_punctuation)
阅读全文