From 59b3fa7d658cfaefbb991da9dd5cf317538fab8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cd-jian2233=E2=80=9D?= <“2528338161@qq.com”> Date: Tue, 11 Jun 2024 15:48:25 +0800 Subject: [PATCH] =?UTF-8?q?python=E7=BB=9F=E8=AE=A1=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=8D=95=E8=AF=8D=E6=95=B0=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...15\225\350\257\215\346\225\260\347\233\256.py" | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 "pythonProject/\347\273\237\350\256\241\346\226\207\346\234\254\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\347\233\256.py" diff --git "a/pythonProject/\347\273\237\350\256\241\346\226\207\346\234\254\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\347\233\256.py" "b/pythonProject/\347\273\237\350\256\241\346\226\207\346\234\254\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\347\233\256.py" new file mode 100644 index 0000000..320b179 --- /dev/null +++ "b/pythonProject/\347\273\237\350\256\241\346\226\207\346\234\254\344\270\255\347\232\204\345\215\225\350\257\215\346\225\260\347\233\256.py" @@ -0,0 +1,15 @@ +import re + + +def count_words(text): + # 使用正则表达式移除文本中的标点符号,并转换为小写,然后分割成单词列表 + words = re.findall(r'\b\w+\b', text.lower()) + return len(words) + + +# 示例文本 +sample_text = "Hello, this is a sample text to demonstrate word counting in Python. It includes punctuation!" + +# 调用函数并打印结果 +word_count = count_words(sample_text) +print(f"The number of words in the sample text, excluding punctuation, is: {word_count}")