Python String isdigit() Method
The Python isdigit() method is a string method that returns True if all the characters in a string are digits, otherwise False. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. The method does not support floats.
Here are some examples of using the isdigit() method:
# Example 1: A simple example of digit testing using isdigit() method[^4^][1]
# Variable declaration
str = '12345'
# Calling function
str2 = str.isdigit()
# Displaying result
print(str2)
# Output: True
# Example 2: The isdigit() method will return False if the string contains whitespaces, symbols, or alphabets[^3^][4]
print('$100'.isdigit())
print('100 '.isdigit())
# Output: False
# Output: False
# Example 3: Superscript and subscripts (usually written using Unicode) are also considered digit characters[^3^][4]
print('\u00B2'.isdigit())
print('\u2082'.isdigit())
print('²5'.isdigit())
# Output: True
# Output: True
# Output: False
If you have any questions about this code, you can drop a line in comment.
Comments
Post a Comment