isdigit python що це: як працює метод рядка в Python

isdigit in Python: How the String Method Works

isdigit in Python is a string method that checks whether a value contains only digit characters. It is often used for quick input validation, but it has important limits that are easy to miss.

What isdigit checks in Python

isdigit in Python returns True when every character in the string is a digit. If the string contains even one space, minus sign, decimal point, or letter, the result is False.

The method works only with strings, so it is called like this: “123”.isdigit(). It is not available on an int value, because the number must first be represented as text.

  • “123”.isdigit() → True
  • “12a”.isdigit() → False
  • “12.5”.isdigit() → False
  • “-7”.isdigit() → False

When isdigit fits and when it does not

isdigit in Python works well for simple whole numbers without signs or decimal parts. It is useful when you need to filter a code, age, form field, or any other text that should contain digits only.

The method is not suitable for negative numbers, decimal values, or most real-world number validation tasks. For example, “-15” and “3.14” both fail the check, even though they are valid numbers in a mathematical sense.

A practical use case

isdigit in Python is handy before converting a string to int, because it helps avoid errors when a user enters invalid data.

  • Check whether the string contains only digits.
  • If the result is True, convert it with int().
  • If the result is False, show an error message or ask for the value again.

After the check, it is worth testing a few cases: a normal number, an empty string, a signed value, and a value with spaces. If any of them behaves differently from what you expect, the logic needs extra handling.

How isdigit differs from isnumeric and isdecimal

isdigit in Python is not the only way to check numeric characters. Python also provides isdecimal() and isnumeric(), and they do not always return the same result.

  • isdecimal() is the strictest check and is best for standard decimal digits.
  • isdigit() is broader and may return True for some digit-like characters, not just standard digits.
  • isnumeric() is the broadest and includes even more number-related characters.

For most everyday tasks where you need to check whether user input is a plain digit-only string, isdigit is usually enough. If you need a stricter check for decimal digits only, isdecimal() is the better choice.

Common mistakes when using isdigit

isdigit in Python is often used too literally, which leads to logic errors. The most common mistake is expecting it to accept negative numbers or decimals.

  • A string with spaces will fail unless you remove them first with strip().
  • A number with a + or sign returns False.
  • A decimal number with a dot also returns False.
  • An empty string always returns False.

The safer approach is to decide first what format you actually need: only integers, signed integers, or numbers with a fractional part. That choice determines whether isdigit is enough or whether it is better to try int() or float() and handle the exception.

Bottom line

isdigit in Python is a simple check that tells you whether a string contains only digit characters. It is useful for fast validation, but it does not replace proper number handling in real applications. If you need signs, decimals, or a more flexible format, a different approach is the better fit.