酷代码 AI
菜单
服务商

Python 编程:轻松统计输入字符中字母、数字、空格及其他字符个数

以下是一个用 Python 实现的程序,用于输入一行字符,并统计字母、数字、空格和其他字符的个数: ```python def count_characters(string): letters = 0 numbers = 0 spaces = 0 others = 0 for char in string: if char.isalpha(): letters += 1 elif char.isdigit(): numbers += 1 elif char.isspace(): spaces += 1 else: others += 1 print("字母个数:", letters) print("数字个数:", numbers) print("空格个数:", spaces) print("其他字符个数:", others) input_string = input("请输入一行字符:") count_characters(input_string) ``` 在这个程序中,我们定义了一个名为`count_characters`的函数,它遍历输入的字符串中的每个字符,并根据字符的类型进行计数。通过`input`函数获取用户输入的字符串,并调用`count_characters`函数进行统计和输出。 ######[AI写代码神器 | 239点数解答 | 2024-10-26 20:53:28]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]