我想计算包括标点符号(,/; /./!/?)在内的字符串数量。计算包括标点符号在内的字数
到目前为止,已经能够仅计算单词的数量,但标点符号未被计数。试图在每个标点符号之前使用替换来给出空格,但它仍然没有被计算。有人可以帮我吗?
我的代码:
import re
input_text = input("Enter the data: ")
final_text = input_text.replace(',',' ,').replace(';',' ;').replace('.',' .').replace('?',' ?').replace('!',' !')
count = len(re.findall(r'\w+', final_text))
print(count)
例如对于此输入
嗨。你好吗?我很好!你呢?再见!
它应该是16包括所有标点符号。但我只得到11
算的话,然后计算标点符号?把它们加起来? – dgg32
你的代码片段中的ident是奇数。 – dgg32
@ dgg32纠正!感谢您指出!是的,把它们加起来很好! –