I am trying to make a function to detect how many digits, letter, spaces, and others for a string.
Here\'s what I have so far:
def count(x):
leng
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
Following code replaces any nun-numeric character with '', allowing you to count number of such characters with function len.
import re
len(re.sub("[^0-9]", "", my_string))
Alphabetical:
import re
len(re.sub("[^a-zA-Z]", "", my_string))
More info - https://docs.python.org/3/library/re.html
def match_string(words):
nums = 0
letter = 0
other = 0
for i in words :
if i.isalpha():
letter+=1
elif i.isdigit():
nums+=1
else:
other+=1
return nums,letter,other
x = match_string("Hello World")
print(x)
>>>
(0, 10, 2)
>>>