YouTip LogoYouTip

Python3 String Isnumeric

Python3 isnumeric() Method

Python3 isnumeric() Method

Python3.x Python3 isnumeric() Method


Description

The isnumeric() method checks whether all characters in the string are numeric characters. Numeric characters include Unicode digits, fullwidth digits (double-byte), Roman numerals, and Han (Chinese) numerals.

Exponents like Β² and fractions like Β½ are also considered numeric.

s = 'Β½'
s = 'u00BD'

Syntax

Syntax for the isnumeric() method:

str.isnumeric()

Parameters

  • None.

Return Value

Returns True if all characters in the string are numeric characters, otherwise returns False.

Example

The following examples demonstrate the use of the isnumeric() method:

Example

#!/usr/bin/python3

str="tutorial2016"

print(str.isnumeric())

str="23443434"

print(str.isnumeric())

The output of the above example is:

False
True

Unicode digits:

Example

#!/usr/bin/python3

#s = 'Β²3455'
s = 'u00B23455'
print(s.isnumeric())

# s = 'Β½'
s = 'u00BD'
print(s.isnumeric())

a = "u0030" # unicode for 0
print(a.isnumeric())

b = "u00B2" # unicode for Β²
print(b.isnumeric())

c = "10km2"
print(c.isnumeric())

The output of the above example is:

True
True
True
True
False

2 Notes

#0 renmu 110***2351@qq.com Reference

The difference between str.isdecimal() and str.isdigit().

The str.isdecimal() function only returns True for decimal numbers, while the str.isdigit() function returns True for other Unicode-supported characters as well.

For more details, you can use the following code to output:

import itertools

line = '-' * 37
print(line)
print("| β„– | isdigit | isdecimal | chr")
print(line)
for number in itertools.chain(range(0x30, 0x3A), range(0x0660, 0x066A), range(0x06F0, 0x06FA), range(0x07C0, 0x07CA), range(0x0966, 0x0970), range(0x09E6, 0x09F0), range(0x0A66, 0x0A70), range(0x0AE6, 0x0AF0), range(0x0B66, 0x0B70), range(0x0BE6, 0x0BF0), range(0x0C66, 0x0C70), range(0x0CE6, 0x0CF0), range(0x0D66, 0x0D70), range(0x0DE6, 0x0DF0), range(0x0E50, 0x0E5A), range(0x0ED0, 0x0EDA), range(0x0F20, 0x0F2A), range(0x1040, 0x104A), range(0x1090, 0x109A), range(0x17E0, 0x17EA), range(0x1810, 0x181A), range(0x1946, 0x1950), range(0x19D0, 0x19DA), range(0x1A80, 0x1A8A), range(0x1A90, 0x1A9A), range(0x1B50, 0x1B5A), range(0x1BB0, 0x1BBA), range(0x1C40, 0x1C4A), range(0x1C50, 0x1C5A), range(0x2070, 0x2071), range(0x2080, 0x2089), range(0x2460, 0x2474), range(0x2474, 0x2488), range(0x2488, 0x249C), range(0x249C, 0x24B0), range(0x24EA, 0x24F5), range(0x24F5, 0x2500), range(0x2776, 0x2780), range(0x2780, 0x278A), range(0x278A, 0x2794), range(0x2CFD, 0x2CFE), range(0x3007, 0x3008), range(0x3021, 0x302A), range(0x3038, 0x303B), range(0x303D, 0x303E), range(0x3192, 0x3196), range(0x3220, 0x3230), range(0x3251, 0x3260), range(0x3280, 0x328A), range(0x32B1, 0x32C0), range(0xA620, 0xA62A), range(0xA8D0, 0xA8DA), range(0xA900, 0xA90A), range(0xA9D0, 0xA9DA), range(0xAA50, 0xAA5A), range(0xABF0, 0xABFA), range(0xFF10, 0xFF1A)):
    char = chr(number)
    print(f"| {number:04X} | {str(char.isdigit()):7} | {str(char.isdecimal()):9} | {char}")
← Python3 String IsnumericPython3 String Isdigit β†’