Python Forum
How to exclude characters when counting digits - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to exclude characters when counting digits (/thread-11289.html)

Pages: 1 2


How to exclude characters when counting digits - juliabrushett - Jul-02-2018

Hello
I am trying to write a program that will count only numeric digits within a string, excluding all letters. What I have currently counts EVERYTHING. How do I fix this?

def countDigits(digits) :
    digits = 0
    x = len(s)

    for i in range(0, x, 1) :
        ((ord(s[i]) >= 65) and (ord(s[i]) <= 57))
        digits += 1
    return digits


s = input("Enter a string\n")

print("There are", countDigits(s), "digits in your string.")



RE: How to exclude characters when counting digits - j.crater - Jul-02-2018

Try to check if string (character) is a digit with string's isdigit() method.


RE: How to exclude characters when counting digits - juliabrushett - Jul-02-2018

I tried that, but I am returning 0 now (because isdigit() looks for a strings with ALL characters).. How can I fix this?

def countDigits(s) :
    digits = 0
    x = len(s)

    for i in range(0, x, 1) :
        ((ord(s[i]) >= 65) and (ord(s[i]) <= 57))
        digits += 1
        if s.isdigit() == False :
            digits -= 1
            break
        else:
            continue
    return digits


s = input("Enter a string\n")

print("There are", countDigits(s), "digits in your string.")



RE: How to exclude characters when counting digits - gruntfutuk - Jul-02-2018

As you are dealing with a string, you can iterate through it with just:

for character in s:
character takes each character in turn in the loop.

As isdigit() returns True or False, you only need:

if character.isdigit():
    digit += 1
inside the loop.

For future information, you could replace it all with just a return line:

return sum(c.isdigit() for c in s)



RE: How to exclude characters when counting digits - buran - Jul-02-2018

more efficient approach
import string

def smart_count_digits(my_string):
    orig_length = len(my_string)
    for digit in string.digits:
        my_string = my_string.replace(digit, '')
    return orig_length - len(my_string) 



RE: How to exclude characters when counting digits - woooee - Jul-02-2018

You can also use the string version of a digit (there is no such thing as "only numeric digits within a string", they are all strings)
def countDigits(digits) :
    digits = 0
    for ch in digits :
        ## assumes zero is not a digit
        ## and there are no negative numbers
        if "1" <= ch <= "9":
            digits += 1
    return digits
 
s = input("Enter a string\n")
 
print("There are", countDigits(s), "digits in your string.")  



RE: How to exclude characters when counting digits - volcano63 - Jul-02-2018

Since you were given a lot of advice, just some details about this code
(Jul-02-2018, 05:50 AM)juliabrushett Wrote:
def countDigits(s) :
    digits = 0
    x = len(s)

    for i in range(0, x, 1) :
        ((ord(s[i]) >= 65) and (ord(s[i]) <= 57)) <----- This line does nothing
        digits += 1
        if s.isdigit() == False :
            digits -= 1
            break
        else:   <--- This is parasitic
            continue 
    return digits


s = input("Enter a string\n")

print("There are", countDigits(s), "digits in your string.")



RE: How to exclude characters when counting digits - nilamo - Jul-02-2018

(Jul-02-2018, 05:01 AM)juliabrushett Wrote:
        ((ord(s[i]) >= 65) and (ord(s[i]) <= 57))
        digits += 1

What do you think that does? There's no if there, so counting every character in the string as a digit.


RE: How to exclude characters when counting digits - volcano63 - Jul-02-2018

Couple of more things
  • you never compare Boolean to True or False - because the result will be ... Boolean. For negating Boolean, use operator not
  • You may check that a value lies between to boundaries in an easier way
    57 <= ord(c) <= 65



RE: How to exclude characters when counting digits - gruntfutuk - Jul-03-2018

(Jul-02-2018, 12:16 PM)buran Wrote: more efficient approach
import string

def smart_count_digits(my_string):
    orig_length = len(my_string)
    for digit in string.digits:
        my_string = my_string.replace(digit, '')
    return orig_length - len(my_string) 

Just checking, as I thought my preceding:

def count_func(s):
    return sum(c.isdigit() for c in s)
would be pretty efficient, but I didn't run time it.