Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
expandtabs() method in Python
Next article icon

The isnumeric() method is a built-in method in Python that belongs to the string class. It is used to determine whether the string consists of numeric characters or not. It returns a Boolean value. If all characters in the string are numeric and it is not empty, it returns “True” If all characters in the string are numeric characters, otherwise returns “False”.

Example: In this given string we will check string contains numeric characters or not.

Python3
string="123456789"result=string.isnumeric()print(result)

Output:

True

Python String isnumeric() Method Syntax

Syntax:  string.isnumeric()

Parameters:isnumeric() does not take any parameters

Returns :

  • True - If all characters in the string are numeric characters.
  • False - If the string contains 1 or more non-numeric characters.

Ways to Implement the isnumeric() Method in Python

In Python, there are different libraries, functions, and methods to check if strings contain numeric characters. Here are the different ways in which we can use Isnumeric method.

Checking numeric/non-numeric characters using isnumeric() Method in Python

Python3
string='123ayu456'print(string.isnumeric())string='123456'print(string.isnumeric())

Output: 

FalseTrue

We can use various methods to check if the string contains numeric characters or not. To check this we can use different approach to solve this.

Counting and Removing numeric characters

In this example, the isnumeric() method is used to check the number of numeric characters and the resulting string after removing numeric characters.

Python3
# Given stringstring='123geeks456for789geeks'count=0new_string=""forchinstring:ifch.isnumeric():count+=1else:new_string+=chprint("Number of numeric characters:",count)print("String after removing numeric characters:",new_string)

Output: 

Number of numeric characters: 9String after removing numeric characters: geeksforgeeks

Errors and Exceptions

It does not contain any arguments, therefore, it returns an error if a parameter is passed.

Python3
# isnumeric() returns an error if a parameter is passedString="1234567"try:String.isnumeric("abc")exceptTypeError:print("TypeError: isnumeric() takes no arguments (1 given)")

Output
TypeError: isnumeric() takes no arguments (1 given)

White spaces are not considered to be numeric, therefore, it returns "False".

Python3
# isnumeric() to check White-spacess=" "p="12 3"print(s.isnumeric())# Falseprint(p.isnumeric())# False# This code is contributed by Susobhan Akhuli

Output
FalseFalse

Subscript, Superscript, Fractions, and Roman numerals (all written in Unicode)are all considered to be numeric, Therefore, it returns "True".

Python3
string1='123'string2='⅓'string3='²'string4='2167'# 'Ⅷ'; ROMAN NUMERAL EIGHTprint(string1.isnumeric())# Trueprint(string2.isnumeric())# Trueprint(string3.isnumeric())# Trueprint(string4.isnumeric())# True

Output
TrueTrueTrueTrue

Combining isnumeric() with conditions

In this example, the isnumeric() method is used to check if the string "75" consists of only numeric characters.

Python3
string='75'ifstring.isnumeric()andint(string)>50:print("Valid Number")else:print("Invalid Number")

Output: 

Valid Number

String isnumeric() with another numeric type

The isnumeric() method in Python is primarily designed to work with strings. In this example, we can see the isnumeric() method may not directly support other numeric types like integers or floats, but still can utilize in combination with type conversion to perform numeric validation

Python3
# integer validationnumber=75string=str(number)result=string.isnumeric()print(result)# float validationnumber=5.65string=str(number)result=string.replace('.','',1).isnumeric()print(result)

Output: 

TrueTrue

Improve

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