×

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 Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Best Way to Learn Python for Free

Python is a booming language these days. It has many applications for making code easier; it is also an open-source language. Learning Python is a step towards coding. Python gets...

5 minutes read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

How to make a firewall in Python?

Firewall: The firewall is a network which controls the incoming and outgoing network traffics of a monitor. It blocks the dataset based on the set of rules written in the security...

3 minutes read.

Python Queue

Python Queue There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for...

7 minutes read.

Write a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

5 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Best Python AI Projects

Artificial consciousness is advancing quickly, from Chabot's to self-driving vehicles. Because of the various advantages and development presented by AI, numerous enterprises have begun searching for AI-fueled applications. Thus, there...

7 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Python program to find whether a given number is even or odd

Python program to find whether a given number is even or odd A number is said to be even if any number is completely divisible by 2, which means there is...

1 minute read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Python System Requirements

Introduction As we know, Python is a popular programming language usually used to write scripts for operating systems. It’s handy adequate for utilizing in web development and application design. In this article,...

2 minutes read.

Python Skyline

Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.