Search code examples
pythonstringfunctionuppercaselowercase

Finding the number of upper and lower case letter in a string in python


when I run the code it always shows 0 as results.... CODE:

def case_counter(string):
    lower=0
    upper=0
    for char in string:  
        if (string.islower()):
            lower=lower+1
        
        elif (string.isupper()):
            upper=upper+1
        
    print('the no of lower case:',lower)
    print('the no of upper case',upper)
string='ASDDFasfds'        
case_counter(string)

RESULT: the no of lower case: 0 the no of upper case 0 EXPECTED: the no of lower case:5 the no of upper case 5


Solution

  • When comparing lower and higher value for your purpose you have to use "char" variable Like the example in this code

    def case_counter(string):
        lower=0
        upper=0
        for char in string:  
            if (char.islower()):
                lower=lower+1
            
            elif (char.isupper()):
                upper=upper+1
            
        print('the no of lower case:',lower)
        print('the no of upper case',upper)
    string='ASDDFasfds'        
    case_counter(string)