Count Digits in String

Count digit characters in string.

BeginnerPhase 5 - Category 2: Character OperationsExample 6 of 10
count-digits-in-string.py
1# Take string input
2s = input("Enter a string: ")
3
4# Count digits
5count = 0
6for char in s:
7 if char.isdigit():
8 count += 1
9
10print(f"Digits: {count}")

Output

Enter a string: Hello123
Digits: 3

What's going on

Check each character if digit.

Key Concepts:

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