Python String isdigit() Method

11-02-23 Ahmed Obaid 1019 0

​The isdigit() function checks if all characters in a string are numeric characters. If all characters in the string are numeric characters, return the boolean value True. If otherwise, it returns the boolean value False.


How to formulate it like this:


string. isdigit()

Parameters: The isdigit() function takes no parameters.

return value:

True: If all characters in the string are numeric characters.

False: If the string contains one or more non-numeric characters.

Errors and exceptions:

If a parameter is passed to the isdigit() function, an error occurs.


If the string contains only numbers, isdigit() will return the boolean value True. If the string contains white spaces, symbols, or alphabets, isdigit() will return the boolean value False.

Example:


# Define a variable consisting of numeric characters
string = "123456"
#isalnum Call and print a function
print(string.isdigit()) # returns True
# Define a variable composed of alphanumeric characters
string = "TEL123456"
#isalnum Call and print a function
print(string.isdigit()) # returns False
# Define a variable composed of letters of the alphabet
string = "AHMED"
#isalnum Call and print a function
print(string.isdigit()) # returns False
# Define a variable composed of numeric characters and symbols
string = "1$"
#isalnum Call and print a function
print(string.isdigit()) # returns False
# Define a variable that contains blank spaces
string = "123 "
#isalnum Call and print a function
print(string.isdigit()) # returns False

 The output will be:


True
False
False
False
False

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python python data types Python String string methods python String comparison functions in Python Python String isdigit Method

Share page