Skip to content

Commit 910a1be

Browse files
authored
[Edit] Python Strings: .isdigit() (#6610)
1 parent 6f2d714 commit 910a1be

File tree

1 file changed

+52
-10
lines changed
  • content/python/concepts/strings/terms/isdigit

1 file changed

+52
-10
lines changed
Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
---
22
Title: '.isdigit()'
3-
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.'
44
Subjects:
5-
- 'Data Science'
65
- 'Computer Science'
6+
- 'Data Science'
77
Tags:
8-
- 'Strings'
9-
- 'Methods'
8+
- 'Characters'
109
- 'Functions'
10+
- 'Methods'
11+
- 'Strings'
1112
CatalogContent:
1213
- 'learn-python-3'
1314
- 'paths/analyze-data-with-python'
1415
---
1516

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.
1720

1821
## Syntax
1922

2023
```pseudo
2124
string.isdigit()
2225
```
2326

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:**
2530

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:
2736

2837
```py
2938
my_string1 = "123"
@@ -33,18 +42,51 @@ print(my_string1.isdigit())
3342
print(my_string2.isdigit())
3443
```
3544

36-
This results in the following output:
45+
The output for the example will be:
3746

3847
```shell
3948
True
4049
False
4150
```
4251

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
4469

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`:
4671

4772
```codebyte/python
4873
my_string = "!--1234--\n"
74+
4975
print(my_string.isdigit())
5076
```
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

Comments
 (0)