We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 81cd29b + 5625194 commit 0d0c7dfCopy full SHA for 0d0c7df
has_only_digits.py
@@ -1,18 +1,16 @@
1
-# Given a string, check if it only contains digits
+# Given a string, check if it only contains digits.
2
3
def is_digit(input_str):
4
- digits = dict()
5
-
6
- for char in input_str:
7
- if char in digits:
8
- digits[char] += 1
9
- else:
10
- digits[char] = 1
11
12
- if sum(digits.values()) == len(input_str):
13
- return True
14
15
- return False
+ try:
+ # If its possible to convert to a number, return True.
+ int(input_str)
+ return True
+ except ValueError:
+ # If the string contains letters, above will fail, returning False.
+ return False
16
17
result = is_digit("095357973590759530")
18
print(result) # True
+
+result = is_digit("1234abc567")
+print(result) # False
0 commit comments