Count Digits in String

Count digit characters in string.

Logic BuildingBeginner
Logic Building
# Take string input
s = input("Enter a string: ")

# Count digits
count = 0
for char in s:
    if char.isdigit():
        count += 1

print(f"Digits: {count}")

Output

Enter a string: Hello123
Digits: 3

Check each character if digit.

Key Concepts:

  • isdigit() checks if digit
  • Count matching characters
  • Loop through string