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
#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


Messages In This Thread
RE: How to exclude characters when counting digits - by gruntfutuk - Jul-02-2018, 11:34 AM

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