< Prev
Next >

# Python - String isdecimal() Method

The isdecimal() string method returns True if all the characters in a string are decimal numbers i.e. from 0 to 9, otherwise it returns False.

Note : None of the string methods change the original string and only return a new string which reflects the change performed by the string method, hence the returned string must be stored or used(if needed to reflect the result), otherwise it is lost.

## Signature of isdecimal()

The isdecimal() method has no parameters, so we don't have to worry about passing it some value.

``````# Signature of isdecimal() method
isdecimal()``````

• ## Calling isdecimal() method on a string object.

• In the upcoming code, we are going to call the isdecimal() method on multiple string objects and see the result.

``````# Python - Method isdecimal() Example

# Creating the first string
s1 = ('Hey you, be happy!')

# Printing the first string
print('The first string : ', s1)

# Calling  the isdecimal() method on first string object
print('isdecimal() method on first string : ', s1.isdecimal())

# Creating the second string
s2 = ('The first 8 planets')

# Printing the second string
print('The second string : ', s2)

# Calling  the isdecimal() method on second string object
print('isdecimal() method on second string : ', s2.isdecimal())

# Creating the third string
s3 = ('4...1..6..1!')

# Printing the third string
print('The third string : ', s3)

# Calling  the isdecimal() method on third string object
print('isdecimal() method on third string : ', s3.isdecimal())

# Creating the fourth string
s4 = ('862331')

# Printing the fourth string
print('The fourth string : ', s4)

# Calling  the isdecimal() method on fourth string object
print('isdecimal() method on fourth string : ', s4.isdecimal())``````

## Output

``````The first string :  Hey you, be happy!
isdecimal() method on first string :  False
The second string :  The first 8 planets
isdecimal() method on second string :  False
The third string :  4...1..6..1!
isdecimal() method on third string :  False
The fourth string :  862331
isdecimal() method on fourth string :  True``````

## Program Analysis

• Calling the isdecimal() on the first string of all characters, returns False.
• Calling the isdecimal() on the second string of all characters(except 1 digit), returns False.
• Calling the isdecimal() on the third string of all decimal values(except dot characters), returns False.
• Calling the isdecimal() on the fourth string of all decimal values, returns True.

• ## Another example of isdecimal() method

Let us see a few more examples of calling the isdecimal() method on all different forms of strings.

``````# Python - Method isdecimal() Example

# Calling isdecimal() method on a string of decimal values with a comma
print('75,99'.isdecimal())

# Calling isdecimal() method on a string repesenting a specific currency value
print('8.99\$'.isdecimal())

# Calling isdecimal() method on a string representing a floating value
print('82.99'.isdecimal())

# Calling isdecimal() method on a string of all decimal values except a !
print('1000!'.isdecimal())

# Calling isdecimal() method of all decimal values
print('011101'.isdecimal())``````

## Output is :

``````False
False
False
False
True``````

## Program Analysis

• Calling the isdecimal() on the first string representing decimal values with a comma, returns False.
• Calling the isdecimal() on the second string representing a currency value, returns False.
• Calling the isdecimal() on the third string representing a floating value, returns False.
• Calling the isdecimal() on the fourth string of all decimal values, except a 1, returns True.
• Calling the isdecimal() on the first string of all decimal values, returns True.

< Prev
Next >
< String isalnum() Method
String isidentifier() Method >