• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python isdigit() Function – Check if Characters in String are Digits

Python isdigit() Function – Check if Characters in String are Digits

October 6, 2022 Leave a Comment

The Python string isdigit() function checks if all characters in a string are digits and returns a boolean value.

s1 = "123"
s2 = "hello"

print(s1.isdigit())
print(s2.isdigit())

#Output:
True
False

When working with strings in Python, the ability to make checks and determine certain properties easily is very valuable.

One such case is if you want to check if all characters in a string are digits in Python.

The Python string isdigit() function checks if all characters in a string are digits and returns a boolean value.

If all characters are digits, then isdigit() returns True. If at least one character is not a digit, i.e. a letter or symbol, then isdigit() returns False.

Using isdigit() can be useful for data validation if you want to check if a string is an integer or if it has non-numeric characters.

Below are some examples showing you how to use isdigit() in Python to check if a string is made up of all digits.

s1 = "123"
s2 = "hello"
s3 = "hello123"


print(s1.isdigit())
print(s2.isdigit())
print(s3.isdigit())

#Output:
True
False
False

Using isdigit() to Check if String Contains Only Numbers in Python

There are a few special cases you need to be aware of when using isdigit(). These special cases arise when your strings have unicode characters.

For example, superscript and subscripts are considered digit characters. On the other hand, numeric characters, such as roman numerals, currency numerators and fractions, are not digit characters.

Below show some more examples of how isdigit() treats these special cases when your string contain unicode characters.

s2 = "\u00B123"
s3 = "\u00BC"

print(s2)
print(s3)

print(s2.isdigit())
print(s3.isdigit())

#Output:
²123
¼

True
False

Hopefully this article has been useful for you to learn how to use the Python isdigit() function.

Other Articles You'll Also Like:

  • 1.  Truncate String in Python with String Slicing
  • 2.  Python Square Root Without Math Module – ** or Newton’s Method
  • 3.  Open Multiple Files Using with open in Python
  • 4.  What Curly Brackets Used for in Python
  • 5.  How to Iterate through a Set Using Python
  • 6.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 7.  Change Python Turtle Background Color with screensize() Function
  • 8.  How to Group and Aggregate By Multiple Columns in Pandas
  • 9.  How to Split a String in Half Using Python
  • 10.  pandas Drop Columns – Delete Columns from a DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x