In the previous article, we have discussed Python Program for isdecimal() Function
isdigit() Function in Python:
If all of the characters are digits, the isdigit() method returns true; otherwise, it returns False.
Exponents, such as ², are also considered digits.
Syntax:
string.isdigit()
Parameters: This function has no parameters.
Examples:
Example1:
Input:
Given first string = "\u0039" Given second string = "\u00B2"
Output:
The above given first string is : 9 The given first string after applying isdigit() function: True The above given second string is : ² The given second string after applying isdigit() function: True
Example2:
Input:
Given first string = "345.67" Given second string = "python_12345"
Output:
The above given first string is : 345.67 The given first string after applying isdigit() function: False The above given second string is : python_12345 The given second string after applying isdigit() function: False
Program for isdigit() 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 isdigit() function to the given first string that returns a value true if all of the characters are digits, otherwise, it returns False.
- Store it in another variable.
- Print the above-given String.
- Print the above-given string after applying isdigit() function.
- Similarly, do the same for the 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 = "\u0039" # Apply isdigit() function to the given first string that returns a value # true if all of the characters are digits, otherwise, it returns False. # Store it in another variable. rslt_str1 = gvn_fststr.isdigit() # Print the given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying isdigit() function. print("The given first string after applying isdigit() function:", rslt_str1) # Similarly do the same for other string and print the result string. gvn_scndstr = "\u00B2" rslt_str2 = gvn_scndstr.isdigit() print("The above given second string is :", gvn_scndstr) print("The given second string after applying isdigit() function:", rslt_str2)
Output:
The above given first string is : 9 The given first string after applying isdigit() function: True The above given second string is : ² The given second string after applying isdigit() 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 isdigit() function to the given first string that returns a value true if all of the characters are digits, otherwise, it returns False.
- Store it in another variable.
- Print the above-given String.
- Print the above-given string after applying isdigit() function.
- Similarly, do the same for the 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 isdigit() function to the given first string that returns a value # true if all of the characters are digits, otherwise, it returns False. # Store it in another variable. rslt_str1 = gvn_fststr.isdigit() # Print the given String print("The above given first string is :", gvn_fststr) # Print the above given String after applying isdigit() function. print("The given first string after applying isdigit() 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.isdigit() print("The above given second string is :", gvn_scndstr) print("The given second string after applying isdigit() function:", rslt_str2)
Output:
Enter some Random String = 345.67 The above given first string is : 345.67 The given first string after applying isdigit() function: False Enter some Random String = python_12345 The above given second string is : python_12345 The given second string after applying isdigit() function: False
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.