How to count the number of letters in a string without the spaces?

后端 未结 13 988
梦谈多话
梦谈多话 2020-12-31 07:45

This is my solution resulting in an error. Returns 0

PS: I\'d still love a fix to my code :)

from collections import Counter
import          


        
13条回答
  •  [愿得一人]
    2020-12-31 08:28

    def count_letters(word):
        return len(word) - word.count(' ')
    

    Alternatively, if you have multiple letters to ignore, you could filter the string:

    def count_letters(word):
        BAD_LETTERS = " "
        return len([letter for letter in word if letter not in BAD_LETTERS])
    

提交回复
热议问题