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


Messages In This Thread
How to exclude characters when counting digits - by juliabrushett - Jul-02-2018, 05:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Counting the number of occurrences of characters in a string nsadams87xx 1 1,938 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