How to count digits, letters, spaces for a string in Python?

Here’s another option:

s="some string"

numbers = sum(c.isdigit() for c in s)
letters = sum(c.isalpha() for c in s)
spaces  = sum(c.isspace() for c in s)
others  = len(s) - numbers - letters - spaces

Leave a Comment