杰瑞科技汇

Python isdigit和isnotdigit有何区别?

Of course! In Python, there isn't a built-in function called isnotdigit. Instead, you use the built-in string method .isdigit() and apply the logical not operator to it.

Python isdigit和isnotdigit有何区别?-图1
(图片来源网络,侵删)

This is a very common and Pythonic way to check for the opposite of a condition.

The Quick Answer: not my_string.isdigit()

To check if a string is not composed entirely of digits, you simply use the not keyword in front of the .isdigit() method.

my_string = "hello123"
# Check if the string is NOT all digits
if not my_string.isdigit():
    print(f'"{my_string}" contains at least one non-digit character.')
else:
    print(f'"{my_string}" is all digits.')

Output:

"hello123" contains at least one non-digit character.

Detailed Explanation

The .isdigit() Method

First, let's understand what .isdigit() does. This method returns True if all characters in the string are digits and there is at least one character. Otherwise, it returns False.

Python isdigit和isnotdigit有何区别?-图2
(图片来源网络,侵删)

Examples of .isdigit():

"123".isdigit()      # True
"42".isdigit()       # True
"0".isdigit()        # True
"123a".isdigit()     # False (contains 'a')
"12.3".isdigit()     # False (contains '.')
" ".isdigit()        # False (contains a space, and is empty of digits)
"".isdigit()         # False (empty string)

Using the not Operator

The not operator is a logical operator that inverts a boolean value.

  • If my_string.isdigit() is True, then not my_string.isdigit() is False.
  • If my_string.isdigit() is False, then not my_string.isdigit() is True.

This gives you a direct way to check for the "not digit" condition.

Complete Examples

Let's see it in action with various strings.

Python isdigit和isnotdigit有何区别?-图3
(图片来源网络,侵删)
strings_to_test = ["123", "hello", "123abc", "12.3", "98765", "", " 123 "]
print("--- Checking if strings are NOT all digits ---")
for s in strings_to_test:
    # The core logic: not s.isdigit()
    if not s.isdigit():
        print(f'✅ "{s}" is NOT all digits.')
    else:
        print(f'❌ "{s}" IS all digits.')
print("\n--- For comparison: Checking if strings ARE all digits ---")
for s in strings_to_test:
    if s.isdigit():
        print(f'✅ "{s}" IS all digits.')
    else:
        print(f'❌ "{s}" is NOT all digits.')

Output:

--- Checking if strings are NOT all digits ---
❌ "123" IS all digits.
✅ "hello" is NOT all digits.
✅ "123abc" is NOT all digits.
✅ "12.3" is NOT all digits.
❌ "98765" IS all digits.
✅ "" is NOT all digits.
✅ " 123 " is NOT all digits.
--- For comparison: Checking if strings ARE all digits ---
✅ "123" IS all digits.
❌ "hello" is NOT all digits.
❌ "123abc" is NOT all digits.
❌ "12.3" is NOT all digits.
✅ "98765" IS all digits.
❌ "" is NOT all digits.
❌ " 123 " is NOT all digits.

Common Pitfalls and Important Notes

Negative Numbers

.isdigit() returns False for negative numbers because the hyphen is not a digit.

"-123".isdigit()  # False

If you want to check if a string represents a negative integer, you need a more complex check, often using .isnumeric() or a regular expression.

Decimal Numbers

As shown above, .isdigit() returns False for decimal numbers because the decimal point is not a digit.

"12.34".isdigit()  # False

Other Digit-like Characters (Unicode)

.isdigit() is quite strict. It only recognizes characters that are digits in the Unicode Nd category (e.g., 0-9 in various scripts, and full-width digits like ).

# Full-width digits are considered digits
"123".isdigit()  # True (These are full-width 1, 2, 3)
# Roman numerals are NOT digits
"IV".isdigit()     # False

The .isnumeric() Method

There is a related method called .isnumeric(). It's similar to .isdigit() but is broader. It returns True for characters that represent numeric values, including fractions, Roman numerals, and other numeric symbols from different languages.

"½".isnumeric()    # True
"Ⅳ".isnumeric()    # True (Roman numeral 4)
"123".isnumeric()  # True

You can also use not s.isnumeric() if you need a broader definition of "not a number".

Summary: When to Use What

Your Goal Method to Use Example
Is the string only digits? s.isdigit() "123".isdigit() -> True
Is the string NOT only digits? not s.isdigit() not "123a".isdigit() -> True
Is the string a numeric value (broader)? s.isnumeric() "½".isnumeric() -> True
Is the string NOT a numeric value? not s.isnumeric() not "½".isnumeric() -> False
Is the string an integer (optional +/-)? s.lstrip('-').isdigit() s = "-123"; s.lstrip('-').isdigit() -> True

For most cases where you just want to check if a string contains anything other than 0-9, not my_string.isdigit() is the perfect solution.

分享:
扫描分享到社交APP
上一篇
下一篇