Answer to Question #303642 in Python for dip

Question #303642


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]









1
Expert's answer
2022-02-28T01:08:11-0500
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]

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS