Python3 isnumeric() Method

Python3 字符串Python3 Strings


Description

The isnumeric() method checks whether a string consists only of numbers. Numbers can be: Unicode numerals, full-width digits (double-byte), Roman numerals, Chinese numerals.

Exponent-like characters²Fraction-like characters½Also count as numbers.

# s = '½'
s = '\u00BD'

Syntax

Syntax of the isnumeric() method:

str.isnumeric()

Parameters

  • None.

Return Value

Returns True if the string contains only numeric characters, otherwise returns False.

Example

The following example demonstrates the isnumeric() method:

Example

#!/usr/bin/python3

str = "example2016"  
print (str.isnumeric())

str = "23443434"
print (str.isnumeric())

The output of the above example is as follows:

False
True

Unicode numerals:

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 as follows:

True
True
True
True
False

Python3 字符串Python3 Strings

Other Extensions