Define a function named "countCharacter" that accepts a sentence as parameter (string) and calculate the total number of letters, total number of uppercase letters, total number of lowercase letters, total number of digits and total number of characters that is not letter and not digit. The function will then return a list of 5 integers representing: 1. total number of letters 2. number of letters with uppercase 3. number of letters with lowercase 4. number of digits 5. any other character beside letters and digits Suppose the following input is supplied to the function "Hell0 WorlD!!!4567" the function will return a list with the following values [9, 3, 6, 5, 4]
def countCharacter(sentence):
total, upp, low, digt, other = 0,0,0,0,0
for ch in sentence:
if ch.isdigit():
digt += 1
elif ch.isalpha():
total += 1
if ch.islower():
low += 1
else:
upp += 1
else:
other += 1
return [total, upp, low, digt, other]
Comments
Leave a comment