Signup/Sign In

Python String Methods- isdigit(), isnumeric() and isdecimal()

Posted in Programming   LAST UPDATED: JUNE 23, 2023

    In our last few articles, we have been covering various methods for String handling in like checking if a string contains only alphabets or is alphanumeric characters, or checking if the string is in upper case or lower case, etc. Continuing ahead, in this article we will see how the isdigit(), isdecimal(), and isnumeric() methods work.

    Python String Methods- isdigit(), isnumeric() and isdecimal()

    1. The isdigit method

    This is a built-in method that checks the string for the presence of digits (0-9) in it. This method returns True if the string contains only numbers. Also, the string should contain at least one character, since an empty string returns False.

    Syntax of isdigit method

    string.isdigit()

    It doesn't take any parameters and the string refers to the string which has to be checked for containing digits. It returns True if all the characters in the string are digits and return False if it is empty or has other characters like alphabets or special characters. Let's see a few use cases,

    my_string = '123'
    print(my_string.isdigit())
    
    my_string = '123abc'
    print(my_string.isdigit())
    
    my_string = ''
    print(my_string.isdigit())

    Output:

    True
    False
    False

    Note:

    • If a parameter is passed to this method, it gives a TypeError.

    • Roman numerals, fractions, and currencies are not considered as digits. Hence, this method will return False for such strings.

    • The subscript, superscript, and decimal characters are considered to be digits. Hence, this method will return True for such strings.


    2. The isnumeric method

    This is a built-in method that checks the string for the presence of numeric values. The numeric values refer to Unicode characters which include integers, subscripts, superscripts, Roman numerals, and fractions.

    Syntax of isnumeric method

    string.isnumeric()

    It doesn't take any parameters and the string refers to the string which has to be checked for containing numeric values. It returns True if the characters are numerical values and returns False, if the string is empty or has other characters like alphabets and special characters.

    Time for an example:

    my_string = '\u00BC'
    print(my_string.isnumeric())
    
    my_string = '123abx'
    print(my_string.isnumeric())
    
    my_string = '1230579'
    print(my_string.isnumeric())
    
    my_string = ''
    print(my_string.isnumeric())

    Output:

    True
    False
    True
    False

    Note:

    • If a parameter is passed to this method, it gives a TypeError.

    • Whitespaces are not numerics, hence returning False.

    • The integers, subscripts, superscripts, fractions, and Roman numerals in Unicode are considered to be numerics. Hence, returns True.


    3. The isdecimal method

    This method checks the characters for the presence of decimal numbers/characters.

    Syntax of isdecimal method

    string.isdecimal()

    It doesn't take any parameters and returns True if the string contains decimal numbers and False if the string is empty or if there are nondecimal numbers present in the string. Let's take a code example,

    my_string = '123780264'
    print(my_string.isdecimal())
    
    my_string = '12 34'
    print(my_string.isdecimal())
    
    my_string = ''
    print(my_string.isdecimal())

    Output:

    True
    False
    False

    Note:

    • If a parameter is passed to this method, it gives an Error.

    • Whitespaces are not decimals, hence returning False.

    • The superscript, subscript, currency numerators, fractions, and Roman numerals are not considered as decimal characters in Unicode. Hence, returns False.


    Conclusion

    Python's isdigit(), isnumeric(), and isdecimal() string methods offer invaluable functionality for validating and manipulating numerical strings. By understanding their distinctions and nuances, you can confidently handle numeric input and ensure accurate data processing in your Python programs.

    As you continue your Python programming journey, remember to leverage the power of isdigit(), isnumeric(), and isdecimal() to harness the full potential of your string manipulation capabilities.

    In today's post, we understood how a string can be checked for the presence of digits, numeric characters, and decimal characters. Don't forget to meddle with the code to see what works and what doesn't.

    Frequently Asked Questions(FAQs)

    1. What is the difference between isdigit(), isnumeric(), and isdecimal() in Python?

    The key difference lies in the types of numeric strings they accept. isdigit() returns True if all characters in the string are digits, while isnumeric() is more inclusive and considers other numeric characters like fractions and subscripts. isdecimal(), on the other hand, only recognizes decimal digits.

    2. What does isdigit() method check in Python?

    The isdigit() method in Python checks whether all characters in a string are digits. It returns True if the string is entirely composed of digits and False otherwise.

    3. What does isnumeric() method do in Python?

    The isnumeric() method in Python determines if all characters in a string are numeric. It returns True if the string contains numeric characters, including digits, fractions, and subscripts, and False otherwise.

    4. What is the purpose of the isdecimal() method in Python?

    The isdecimal() method in Python checks if all characters in a string are decimal digits (0-9). It returns True if the string consists of only decimal digits, and False otherwise.

    5. Can these string methods handle floating-point numbers or negative numbers?

    No, none of these methods handle floating-point numbers or negative numbers. They are designed to work with whole numbers or strings representing positive integer values. For handling more complex numeric validation, you may need to use other techniques or specific libraries in Python.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:python
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS