Python Forum
How to exclude characters when counting digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to exclude characters when counting digits
#1
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.")
Reply
#2
Try to check if string (character) is a digit with string's isdigit() method.
Reply
#3
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.")
Reply
#4
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)
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
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) 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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.")  
Reply
#7
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.")
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#8
(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.
Reply
#9
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#10
(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.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,915 Jun-16-2020, 07:22 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020