×

Python String isdigit() method

Python String isdigit() method

The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false.

Syntax

string.isdigit()

Parameter

NA

Return

This method returns a Boolean value true if all characters in the string are digits else for even one non-digit character it returns False.

Example 1

 # Python program explaining
 # the isdigit() method
 # initializing the string value
 strVal = "50800"
 # checking whether the passed string is a digit or not
 result = strVal.isdigit()
 # printing the isdigit() result
 print("The isdigit() method returns: ",result) 

Output

The isdigit() method returns:  True

Example 2

 # Python program explaining
 # the isdigit() method
 # initialising the string value
 strVal = "M0n1caG3ll3r"
 print("String 1:",strVal)
 # validating if and else condition
 if strVal.isdigit() == True:
    print("All characters of specified string are digits.")
 else:
     print("All the characters are not digits.")
 # passing another string
 strVal1="123234675"
 print("String 2:",strVal1)
 # again validating if and else condition
 if strVal1.isdigit() == True:
    print("All characters of specified string are digits.")
 else:
     print("All the characters are not digits.") 

Output

 String 1: M0n1caG3ll3r
 All the characters are not digits.
 String 2: 123234675
 All characters of specified string are digits. 

Example 3

 # Python program explaining
 # the isdigit() method
 strVal1 = '23455'
 print(strVal1.isdigit())
 #s = '²3455'
 # subscript is a digit
 strVal2 = '\u00A223455'
 print(strVal2.isdigit())
 # s = '½'
 # fraction is not a digit
 strVal3 = '\u00BD'
 print(strVal3.isdigit())
 #passing special characters in the string 
 strVal4 = '!@9876/0'
 print(strVal4.isdigit()) 

Output

 True
 False
 False
 False 

Related Topics

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

How to find square root in python

How to find Square Root of a number In Python Python makes a lot of tasks easier by using different functions, modules, and libraires. There is an inbuilt function in Python...

6 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

1 minute read.

Python tokens and character set

In this tutorial, we will understand what are character sets used in python and what is meant by python tokens and we will further discuss the type of tokens being...

4 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Keyboard Jump Game in Python

Keyword hop (jump) game is a speed composing game that aides in further developing the composing velocity of players The object of Keyboard Jump (hop) Game Python Project is to fabricate...

7 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Class and Static Methods in Python

The method is an important concept to learn for every programmer or data analyst. We know that in OOP's concept, each object has its attributes and behaviors defined through methods....

3 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

Python datetime

Python provides a module named datetime to work with the date and time. Sometimes in the real life application development, we need to work with the date and time. The date is...

3 minutes read.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 minutes read.

Run exec python from PHP

PHP (which is a recursive abbreviation for PHP Hypertext Preprocessor) is one of the most broadly involved web improvement innovations on the planet. PHP code utilized for creating sites and...

4 minutes read.

Parameter Passing in Python

In Python, when we characterize capabilities with default values for specific boundaries, it is said to have its contentions set as a possibility for the client. Clients can either pass their...

3 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.