You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Checks if all the elements in the string are digits and returns a boolean flag.'
3
+
Description: 'Checks if all the elements in the string are digits and returns a boolean flag based on the result.'
4
4
Subjects:
5
-
- 'Data Science'
6
5
- 'Computer Science'
6
+
- 'Data Science'
7
7
Tags:
8
-
- 'Strings'
9
-
- 'Methods'
8
+
- 'Characters'
10
9
- 'Functions'
10
+
- 'Methods'
11
+
- 'Strings'
11
12
CatalogContent:
12
13
- 'learn-python-3'
13
14
- 'paths/analyze-data-with-python'
14
15
---
15
16
16
-
The **`.isdigit()`** method under the string class checks if all the elements in the string are digits; applicable elements also include special cases like compatibility superscript digits (¹, ², ³, etc.). The method returns `True` in the mentioned cases and must not be an empty string, otherwise, it returns `False`.
17
+
The **`.isdigit()`** method under the `string`[class](https://www.codecademy.com/resources/docs/python/classes) checks if all the elements in the string are digits; applicable elements also include special cases like superscript digits (¹, ², ³, etc.). The method returns `True` in the mentioned cases and `False` otherwise.
18
+
19
+
This method is commonly used in input validation, data cleaning, and parsing operations where it is necessary to ensure that certain fields - like IDs, phone numbers (without formatting), or user-entered data - consist solely of digits. This method helps avoid errors that might occur from converting non-numeric strings to integers.
17
20
18
21
## Syntax
19
22
20
23
```pseudo
21
24
string.isdigit()
22
25
```
23
26
24
-
## Example
27
+
In the syntax, `string` needs to be replaced with the name of the string on which the `.isdigit()` method is to be called.
28
+
29
+
**Return value:**
25
30
26
-
The code below uses `.isdigit()` to check if a string contains only digits:
31
+
The method returns `True` if all the characters in a string are digits and `False` otherwise.
32
+
33
+
## Example 1: Basic Usage of `.isdigit()`
34
+
35
+
This example uses the `.isdigit()` method to check if the given strings only contains digits:
27
36
28
37
```py
29
38
my_string1 ="123"
@@ -33,18 +42,51 @@ print(my_string1.isdigit())
33
42
print(my_string2.isdigit())
34
43
```
35
44
36
-
This results in the following output:
45
+
The output for the example will be:
37
46
38
47
```shell
39
48
True
40
49
False
41
50
```
42
51
43
-
## Codebyte Example
52
+
## Example 2: Using `.isdigit()` on a String with Letters
53
+
54
+
This example uses the `.isdigit()` method on a string containing alphabetical characters:
55
+
56
+
```py
57
+
text ="123abc"
58
+
59
+
print(text.isdigit())
60
+
```
61
+
62
+
The output for the example will be:
63
+
64
+
```shell
65
+
False
66
+
```
67
+
68
+
## Codebyte Example: Using `.isdigit()` on a String with Special Characters
44
69
45
-
The code below is runnable and uses `.isdigit()`to check `my_string`:
70
+
This codebyte example uses the `.isdigit()`method on a string containing special characters like `!`, `-`, `\`, and `n`:
46
71
47
72
```codebyte/python
48
73
my_string = "!--1234--\n"
74
+
49
75
print(my_string.isdigit())
50
76
```
77
+
78
+
Since these special characters are not considered digits, the output for the example will be `False`.
79
+
80
+
### Frequently Asked Questions
81
+
82
+
### 1. What is the difference between `.isdigit()` and `.isnumeric()` in Python?
83
+
84
+
Both `.isdigit()` and [`.isnumeric()`](https://www.codecademy.com/resources/docs/python/strings/isnumeric) return `True` for digit characters. However, `.isnumeric()` recognizes a broader set of numeric characters, including fractions, subscripts, superscripts, and other Unicode characters representing numbers. For example, the Unicode character `\u00B2` (superscript 2) is considered numeric but not a digit.
85
+
86
+
### 2. What is the difference between `.isdigit()` and `.isdecimal()` in Python?
87
+
88
+
[`.isdecimal()`](https://www.codecademy.com/resources/docs/python/strings/isdecimal) is more restrictive than `.isdigit()`. It only returns `True` for characters that are strictly decimal numbers (0–9). In contrast, `.isdigit()` includes characters like superscripts.
89
+
90
+
### 3. Does `.isdigit()` work for floats?
91
+
92
+
No, `.isdigit()` returns `False` for strings representing float numbers because of the presence of a decimal point, which is not considered a digit.
0 commit comments