isdigit()

my_str.isdigit())
Returns True if all chars are digits in a string, Otherwise False is returned.
my_str='012345'
print(my_str.isdigit())      # Output is True

my_str='12#34'
print(my_str.isdigit())      # Output is False

my_str='abc1234'
print(my_str.isdigit())      # Output is False

my_str='\u0035'              # Unicode of digit 5 
print(my_str.isdigit())      # Output is True

Difference between isnumeric(), isdigit() and isdecimal()

Use google to search translate 1234 to chinese , then use the output like this.
my_str='一千二百三十四'
print(my_str.isnumeric())  # True
print(my_str.isdigit())    # False 
print(my_str.isdecimal())  # False
One more example
my_str = '2²'
print(my_str.isnumeric())  # True
print(my_str.isdigit())    # True 
print(my_str.isdecimal())  # False
my_str = '½'
print(my_str.isnumeric())  # True
print(my_str.isdigit())    # False 
print(my_str.isdecimal())  # False
isdigit() will allow only digits 0-9

Example 1: Handling Unicode Characters

print("²".isdigit())  # Output: True (Superscript)
print("٣".isdigit())   # Output: True (Arabic numeral)

Example 2: Validating User Input

user_input = input("Enter your age: ")
if user_input.isdigit():
    print("Valid age")
else:
    print("Invalid input")

Example 3: Mixed Strings with Digits and Letters

text = "123abc"
print(text.isdigit())  # Output: False (contains letters)

Example 4: Strings with Spaces

text = "123 456"
print(text.isdigit())  # Output: False (contains spaces)

Example 5: Strings with Decimal Points

text = "12.34"
print(text.isdigit())  # Output: False (contains a decimal point)
All String methods


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer