How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

前端 未结 8 2060
礼貌的吻别
礼貌的吻别 2020-11-28 06:45

How would I go about counting the words in a sentence? I\'m using Python.

For example, I might have the string:

string = \"I     am having  a   very         


        
8条回答
  •  暖寄归人
    2020-11-28 07:16

    How about using a simple loop to count the occurrences of number of spaces!?

    txt = "Just an example here move along" 
    count = 1
    for i in txt:
    if i == " ":
       count += 1
    print(count)

提交回复
热议问题