Trying to count words in a string

后端 未结 7 774
醉话见心
醉话见心 2021-02-06 03:28

I\'m trying to analyze the contents of a string. If it has a punctuation mixed in the word I want to replace them with spaces.

For example, If Johnny.Appleseed!is:a*good

7条回答
  •  遥遥无期
    2021-02-06 03:54

    I know that this is an old question but...How about this?

    string = "If Johnny.Appleseed!is:a*good&farmer"
    
    a = ["*",":",".","!",",","&"," "]
    new_string = ""
    
    for i in string:
       if i not in a:
          new_string += i
       else:
          new_string = new_string  + " "
    
    print(len(new_string.split(" ")))
    

提交回复
热议问题