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

    Join Date
    Oct 2013
    Rep Power

    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 God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power
    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