×

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

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

What are the Purposes of Python?

Python is a high-level programming language that is simple to use and easy to write, and Python is a beginner-friendly programming language. A beginner often prefers to start with Python...

6 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Handling missing keys in Python dictionaries

In this lesson, you will discover how to create a Python application that will manage missing keys in a dictionary. Python dictionaries store data as key-value pairs, where each value...

3 minutes read.

Python e-book free download

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. There are many sources to learn python. In this...

3 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

Python isinstance() function

Python isinstance() function The isinstance() function in Python returns a Boolean value ‘True’ if the given object is of the specified type, otherwise it returns False. Syntax isinstance(object, classinfo) Parameter object: It is a required parameter which represents an object. classinfo: This...

1 minute read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

Python Static Method

Introduction to methods that are majorly used in Python: Class MethodsInstance MethodsStatic Methods Before going to the in-depth concept of Static Methods, let us briefly discuss Class Methods and Instance Methods. Class Methods The...

4 minutes read.

Difference between Module and Package in Python

The main difference between the module and the package in Python is that the module can be a simple file in Python that contains the different functions and collection of...

4 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.