In this tutorial, we’ll learn what Python string isnumeric() method is and how to properly use it. We’ll go through multiple Python code examples to understand how isnumeric() method works in Python.
Outline
- Introduction: Python String isnumeric() Method
- Syntax of isnumeric() Method
- Example 1: Apply isnumeric() Method on a Python String
- Example 2: isnumeric() Method Return Value
- Conclusion
Introduction: Python String isnumeric() Method
This method will return True if a string contains only numeric characters and will return False if atleast one character is non-numeric.
Syntax of isnumeric() Method
string.isnumeric()
- Python string isnumeric() method does not take any arguments.
- It returns True if a string has only numeric characters and will return False if atleast one character is non-numeric.
Example 1: Apply isnumeric() Method on a Python String
stringVal='2344693' print(stringVal.isnumeric())
Output
True
Python string isnumeric() method return True as the specified string has only numeric characters.
See below examples in which isnumeric() method is applied to strings containing non-numeric characters as well.
print('P2344693'.isnumeric()) print('Python34'.isnumeric()) print('34_3'.isnumeric()) print('3 3'.isnumeric()) print('86&'.isnumeric())
Output
False False False False False
Python treats mathematical characters like superscripts, subscripts, numbers and characters having Unicode numeric value properties (like a roman numerals, fraction, currency numerators) as numeric characters. Python string isnumeric() method will return True if these characters are used in string(when no non-numeric characters are present). See below examples:
print('²479'.isnumeric()) // superscript value print('2343½'.isnumeric()) // fraction value print('²'.isnumeric()) print('½'.isnumeric())
Output
True True True True
Example 2: isnumeric() Method Return Value
stringVal='44693' returnVal=stringVal.isnumeric() print(returnVal)
Output
True
Python string isnumeric method return True as the specified string has only numeric characters present in it.
Conclusion
To conclude this tutorial, hope you now have a detailed practical knowledge of how to properly use Python string isnumeric method. I’ll be looking forward to have your valuable feedback. Thank you for reading this post.
You may like to read:
How To Use Python String IsLower() Method – Easy Python Code Examples
How To Use Python String IsDecimal() Method – Easy Python Code Guide
How To Use Python String IsDigit() Method – Easy Python Code Guide
How To Use Python String Find() Method – Easy Python Code Guide
How To Use Python String IsAlpha() Method – Easy Python Code Guide