#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    How many digits in a string?


    I need to return the number of digits and letters in any given string. I need to use a basic loop with no advanced techniques. think basic. Here is what i have but it wont work...help is
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Code:
    def digitCount(sentence):
        '''
            moved the return statement.
            You counted only the first character.
        '''
        dig = 0
        for char in sentence:
            if char.isdigit():
                dig += 1
        return dig  
    
    
    # for long sentences this might run faster because it involves less python interpretation and more c
    digitCountDWL = lambda sentence: sum(sentence.count(d) for d in '0123456789')
    Then again, I didn't test. Might not work at all.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo