How to count the letters, spaces, numbers and other characters of a string in Python

1 Answer

0 votes
s = "Python Pro $100%     Prog()ramming   99 !!!"

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


print("numbers:", numbers)
print("letters:", letters)
print("spaces:", spaces)
print("others:", others)




'''
run:

numbers: 5
letters: 20
spaces: 11
others: 7

'''

 



answered Aug 7, 2021 by avibootz
...