In the previous article, we have discussed Python Program for gamma() Function
isdecimal() Function in Python:
If all of the characters are decimals(0-9), the isdecimal() method returns True.
On unicode objects, this method is used.
Syntax:
string.isdecimal()
Parameters: This function has no parameters.
Examples:
Example1:
Input:
Given first string = "123456789_python" Given second string = "92567"
Output:
The above given first string is : 123456789_python The given first string after applying isdecimal() function: False The above given second string is : 92567 The given second string after applying isdecimal() function: True
Example2:
Input:
Given first string = "\u0039" (#unicode for 9) Given second string = "92567"
Output:
The above given first string is : 9 The given first string after applying isdecimal() function: True The above given second string is : 92567 The given second string after applying isdecimal() function: True
Program for isdecimal() Function in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the first string as static input and store it in a variable.
- Apply isdecimal() function to the given first string that returns a value true if all of the characters are decimals(0-9).
- Store it in another variable.
- Print the given String.
- Print the above-given String after applying isdecimal() function.
- Similarly, do the same for other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as static input and store it in a variable. gvn_fststr = "123456789_python" # Apply isdecimal() function to the given first string that returns a value # true if all of the characters are decimals(0-9). # Store it in another variable. rslt_str1 = gvn_fststr.isdecimal() # Print the given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying isdecimal() function. print("The given first string after applying isdecimal() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = "92567" rslt_str2 = gvn_scndstr.isdecimal() print("The above given second string is :", gvn_scndstr) print("The given second string after applying isdecimal() function:", rslt_str2)
Output:
The above given first string is : 123456789_python The given first string after applying isdecimal() function: False The above given second string is : 92567 The given second string after applying isdecimal() function: True
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the first string as user input using the input() function and store it in a variable.
- Apply isdecimal() function to the given first string that returns a value true if all of the characters are decimals(0-9).
- Store it in another variable.
- Print the given String.
- Print the above-given String after applying isdecimal() function.
- Similarly, do the same for other string and print the result string.
- The Exit of Program.
Below is the implementation:
# Give the first string as user input using the input() function and store it in a variable. gvn_fststr = input("Enter some Random String = ") # Apply isdecimal() function to the given first string that returns a value # true if all of the characters are decimals(0-9). # Store it in another variable. rslt_str1 = gvn_fststr.isdecimal() # Print the given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying isdecimal() function. print("The given first string after applying isdecimal() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = input("Enter some Random String = ") rslt_str2 = gvn_scndstr.isdecimal() print("The above given second string is :", gvn_scndstr) print("The given second string after applying isdecimal() function:", rslt_str2)
Output:
Enter some Random String = goodmorning456288729 The above given first string is : goodmorning456288729 The given first string after applying isdecimal() function: False Enter some Random String = 90876123 The above given second string is : 90876123 The given second string after applying isdecimal() function: True
Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.