python统计字符串单词个数
作者:野牛程序员:2023-11-07 15:29:53python阅读 3017
Python统计字符串单词个数可以使用以下代码:
import re def count_words_in_string(input_string): words = re.findall(r'\\w+', input_string) return len(words) input_string = "这是一个示例字符串,用于统计单词的数量。" word_count = count_words_in_string(input_string)
这段代码使用正则表达式来匹配字符串中的单词,并计算它们的数量。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:python字符串判断纯字母
- 下一篇:python1234组成三位数不重复