'Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters
Question: Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?' Expected Output : No. of Upper case characters : 4 No. of Lower case Characters : 33
function:
def up_low(s):
for a in s:
u = u.count(a.isupper())
l = l.count(a.islowwer())
print(u, l)
why this one doesn't work?
Solution 1:[1]
You can use List comprehensions and sum function to get the total number of upper and lower case letters.
def up_low(s):
u = sum(1 for i in s if i.isupper())
l = sum(1 for i in s if i.islower())
print( "No. of Upper case characters : %s,No. of Lower case characters : %s" % (u,l))
up_low("Hello Mr. Rogers, how are you this fine Tuesday?")
Output: No. of Upper case characters : 4,No. of Lower case characters : 33
Solution 2:[2]
You are understanding wrong the count function, count, first of all, is a string functrion, and as parameter take a string, instead of doing this, you can simply do:
def up_low(string):
uppers = 0
lowers = 0
for char in string:
if char.islower():
lowers += 1
elif char.isupper():
uppers +=1
else: #I added an extra case for the rest of the chars that aren't lower non upper
pass
return(uppers, lowers)
print(up_low('Hello Mr. Rogers, how are you this fine Tuesday?'))
4 33
Solution 3:[3]
Problem
why this one doesn't work?
Along with having syntax errors and run-time errors, you code's logic is quite a ways off. You are actual not doing what the question asked. You seem to be trying to count the number of uppercase characters in a single character. That's incorrect.
Let's look back over the question to implement this correctly:
Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters. Sample String :
'Hello Mr. Rogers, how are you this fine Tuesday?'
Expected Output : No. of Upper case characters :4
No. of Lower case Characters :33
.
Ok, so we have a clear definition of our problem. Given a single string, count the number of lower case characters the string contains, and uppercase characters the string contains. Let's start writing our function.
First we should define a function:
def count_upper_and_lower(string):
I know we'll need two variables; How? Because we need one to count the uppercase letters, and one to count the lowercase letters. So let's initialize those:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
Now what do we need? Well the problem says to count each letter in the string. It sounds like we need to iterate over each character in the string. So we should use a for
loop:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
OK, so what logic do we need in our for loop? Well we need to first check if a letter is upper case. If it is, we need to increment uppercase_letter_count
. If not, we'll then test if the character is lowercase. If so, we'll increment lowercase_letter_count
. Otherwise, we'll do nothing. Here is what that would look like in code:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
Let's add that to our for
loop:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
And were done. All that's left to do is to print the values at the end of the function:
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
print uppercase_letter_count, lowercase_letter_count
Demo
def count_upper_lower(string):
lowercase_letter_count = 0
uppercase_letter_count = 0
for letter in string:
if letter.isupper():
uppercase_letter_count += 1
elif letter.islower():
lowercase_letter_count += 1
print uppercase_letter_count, lowercase_letter_count
count_upper_lower("Hello Mr. Rogers, how are you this fine Tuesday?")
# Output: 4 33
count_upper_lower("The FAT Cat Moaned AlL day!")
# Output: 8 13
Solution 4:[4]
If people are posting code...
def up_low(s):
up = filter(str.isupper, s)
low = filter(str.islower, s)
return len(up), len(low)
In Python 3, each filter will need to be converted into a list or thrown into a sum(1 for _ in ...)
expression:
def up_low(s):
counter = lambda f: sum(1 for c in s if f(c))
return counter(str.upper), counter(str.lower)
Solution 5:[5]
A bit late to the party, but for the sake of completeness:
import string
def up_low(s):
return (sum(map(s.count, string.ascii_uppercase)),
sum(map(s.count, string.ascii_lowercase)))
And to test it:
u, l = up_low('Hello Mr. Rogers, how are you this fine Tuesday?')
print("No. of Upper case characters : {}\nNo. of Lower case Characters : {}".format(u, l))
Which gives:
No. of Upper case characters : 4 No. of Lower case Characters : 33
A manual loop & count is probably faster, tho.
Solution 6:[6]
I think this is the easiest approach for a beginner.
def up_low(s):
a=list(s)
u=[]
l=[]
for x in a:
if x.isupper():
u.append(x)
elif x.islower():
l.append(x)
else:
pass
return u, l
u, l = up_low(s)
print(f'No. of Upper case characters: {len(u)}')
print(f'No. of Lower case Characters: {len(l)}')
Solution 7:[7]
One simple
def up_low(s):
c_upper = 0
c_lower = 0
for letters in s:
if letters.isupper():
c_upper += 1
elif letters.islower():
c_lower += 1
else:
pass
print("Total characters :",len(s.replace(" ", "")),"\nNo. of Upper case characters :",c_upper, "\nNo. of Lower case Characters :",c_lower)
INPUT
mystring = 'Hello Mr. Rogers, how are you this fine Tuesday?'
up_low(mystring)
OUTPUT
Total characters : 40
No. of Upper case characters : 4
No. of Lower case Characters : 33
Solution 8:[8]
def up_low(s):
u, l = [], []
for a in s:
if a.isupper():
u.append(a)
else:
l.append(a)
print(len(u), len(l))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | |
Solution 4 | |
Solution 5 | |
Solution 6 | Tom Carrick |
Solution 7 | Mr Mayur |
Solution 8 | Rahul |