Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Strings encode() method
Next article icon

The isdigit()method is a built-in Python function that checks if all characters in a string are digits. This method returnsTrue if each character in the string is a numeric digit (0-9) andFalse otherwise.Example:

Python
a="12345"print(a.isdigit())b="1234a5"print(b.isdigit())

Output
TrueFalse

Explanation:In this example,a.isdigit()returnsTrue because "12345" consists entirely of numeric characters, whileb.isdigit() returnsFalse due to the non-numeric character "a" in the string.

Syntax of isdigit()

s.isdigit()

Parameters:This method does not take any parameters

Returns:

  • It returns True if every character in the string is a numeric digit (0-9).
  • It returns False if there is any non-numeric character in the string.

Examples of isdigit()

Let's explore some examples of isdigit()method for better understanding.

Example 1: Here are example with a string containing only digits, a mixed-character string and an empty string.

Python
# Only digitsprint("987654".isdigit())# Digits and alphabetprint("987b54".isdigit())# Empty stringprint("".isdigit())

Output
TrueFalseFalse

Explanation:

  • "987654".isdigit()returns True as it contains only numeric digits.
  • "987b54".isdigit() returns False due to the non-digit character 'b'.
  • "".isdigit()returns False because the string is empty.

Example 2: Theisdigit() method can also be used to check numeric characters encoded in Unicode, such as the standard digit '1' and other Unicode representations of numbers.

Python
# Unicode for '1'a="\u0031"print(a.isdigit())# Devanagari digit for '1'b="\u0967"print(b.isdigit())

Output
TrueTrue

Explanation:

  • "\u0031".isdigit()returns True as it represents the standard digit '1'.
  • "\u0967".isdigit() returns True as it represents the Devanagari digit '1'.

Example 3: Here are example with Roman numerals, which are not recognized as digits.

Python
print("I".isdigit())print("X".isdigit())print("VII".isdigit())

Output
FalseFalseFalse

Explanation:"I".isdigit(), "X".isdigit() and "VII".isdigit() all return False as Roman numerals are not recognized as digits by isdigit().

Related Articles:


Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp