Python provides several string methods to determine and check whether a string type is numeric or alphabetic.
Each method is explained with sample code.
- Determines whether a string is a decimal digit:
str.isdecimal()
- Determining if a string is a number:
str.isdigit()
- Determines whether a string is a character representing a number:
str.isnumeric()
- Determines if string is alphabetic:
str.isalpha()
- Determine if string is alphanumeric:
str.isalnum()
- Determines whether strings are ASCII characters:
str.isascii()
- Judgment of empty string
- Determine if strings can be converted to numbers
For methods other than isascii(), a string containing an empty string, the following symbols, etc., is false.
,
.
-
-1.23, etc., as a numerical value is explained at the end of this section.
Regular expressions can be used to determine character types more flexibly and to extract the relevant character types.
See the following article for more information on how to determine the following
- How to convert a numeric string (str) to a number (int, float)
- How to determine upper and lower case
- RELATED:Converting a string of numbers to numeric values in Python
- RELATED:List of string methods to manipulate case in Python
- Determines whether a string is a decimal digit: str.isdecimal()
- Determining if a string is a number: str.isdigit()
- Determines whether a string is a character representing a number: str.isnumeric()
- Determines if string is alphabetic: str.isalpha()
- Determine if string is alphanumeric: str.isalnum()
- Determines whether strings are ASCII characters: str.isascii()
- Judgment of empty string
- Determine if strings can be converted to numbers
Determines whether a string is a decimal digit: str.isdecimal()
In isdecimal(), it is true if all characters are decimal digits, that is, characters in the general category Nd of Unicode. It is also true for full-width Arabic numerals, etc.
s = '1234567890'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = 1234567890
# isdecimal: True
# isdigit: True
# isnumeric: True
s = '1234567890'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = 1234567890
# isdecimal: True
# isdigit: True
# isnumeric: True
If it contains a symbol such as a minus sign or a period, it is false. For example, if you want to determine that a string such as '-1.23' is a numeric value, you can use exception handling. This is explained at the end of this section.
s = '-1.23'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = -1.23
# isdecimal: False
# isdigit: False
# isnumeric: False
Determining if a string is a number: str.isdigit()
In isdigit(), in addition to numbers that are true in isdecimal(), numbers whose Unicode property value Numeric_Type is Digit or Decimal are also true.
For example, a superscript number representing a square is false in isdecimal() but true in isdigit().
- superscript number representing the square
- ²
- '\u00B2}'
s = '10\u00B2'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = 10²
# isdecimal: False
# isdigit: True
# isnumeric: True
Determines whether a string is a character representing a number: str.isnumeric()
In isnumeric(), in addition to numbers that are true in isdigit(), numbers whose Unicode property value Numeric_Type is Numeric are also true.
Fractions, Roman numerals, and Chinese numerals are also true.
s = '\u00BD'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = ½
# isdecimal: False
# isdigit: False
# isnumeric: True
s = '\u2166'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = Ⅶ
# isdecimal: False
# isdigit: False
# isnumeric: True
s = '一二三四五六七八九〇'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = 一二三四五六七八九〇
# isdecimal: False
# isdigit: False
# isnumeric: True
s = '壱億参阡萬'
print('s =', s)
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = 壱億参阡萬
# isdecimal: False
# isdigit: False
# isnumeric: True
Determines if string is alphabetic: str.isalpha()
In isalpha(), a Unicode general category property with one of the following is true.
Lm
Lt
Lu
Ll
Lo
The alphabet, Chinese characters, etc. will be true.
s = 'abc'
print('s =', s)
print('isalpha:', s.isalpha())
# s = abc
# isalpha: True
s = '漢字'
print('s =', s)
print('isalpha:', s.isalpha())
# s = 漢字
# isalpha: True
Arabic numerals are false, but Chinese numerals are true because they are also Chinese characters; however, zeros in Chinese numerals are false.
s = '1234567890'
print('s =', s)
print('isalpha:', s.isalpha())
# s = 1234567890
# isalpha: False
s = '1234567890'
print('s =', s)
print('isalpha:', s.isalpha())
# s = 1234567890
# isalpha: False
s = '一二三四五六七八九'
print('s =', s)
print('isalpha:', s.isalpha())
# s = 一二三四五六七八九
# isalpha: True
s = '壱億参阡萬'
print('s =', s)
print('isalpha:', s.isalpha())
# s = 壱億参阡萬
# isalpha: True
s = '〇'
print('s =', s)
print('isalpha:', s.isalpha())
# s = 〇
# isalpha: False
Roman numerals are false.
s = '\u2166'
print('s =', s)
print('isalpha:', s.isalpha())
# s = Ⅶ
# isalpha: False
Determine if string is alphanumeric: str.isalnum()
In isalnum(), it is true if each character is true in any of the following methods listed so far.
isdecimal()
isdigit()
isnumeric()
isalpha()
Each character is evaluated individually, so a string containing letters and numbers will be true in isalnum() even if false in all other methods.
s = 'abc123'
print('s =', s)
print('isalnum:', s.isalnum())
print('isalpha:', s.isalpha())
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
# s = abc123
# isalnum: True
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
Determines whether strings are ASCII characters: str.isascii()
Python 3.7 added isascii(). It returns true if all characters in the string are ASCII characters.
In addition to numbers and letters, symbols such as + and – are also true.
s = 'abc123+-,.&'
print('s =', s)
print('isascii:', s.isascii())
print('isalnum:', s.isalnum())
# s = abc123+-,.&
# isascii: True
# isalnum: False
Non-ASCII hiragana and other characters are false.
s = 'あいうえお'
print('s =', s)
print('isascii:', s.isascii())
print('isalnum:', s.isalnum())
# s = あいうえお
# isascii: False
# isalnum: True
As we will see next, unlike the other methods, isascii() returns true even for an empty string.
Judgment of empty string
An empty string is true for isascii() and false for the other methods.
s = ''
print('s =', s)
print('isalnum:', s.isalnum())
print('isalpha:', s.isalpha())
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
print('isascii:', s.isascii())
# s =
# isalnum: False
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
# isascii: True
Use bool() to determine if it is an empty string. The return value is false for an empty string and true otherwise.
print(bool(''))
# False
print(bool('abc123'))
# True
Determine if strings can be converted to numbers
Negative or fractional value strings contain periods or minus signs. Therefore, the result is false for all methods except isascii().
Although true for isascii(), it is not suitable for determining whether a string can be converted to a numeric value, since it is true even if it contains other symbols or alphabetic characters.
s = '-1.23'
print('s =', s)
print('isalnum:', s.isalnum())
print('isalpha:', s.isalpha())
print('isdecimal:', s.isdecimal())
print('isdigit:', s.isdigit())
print('isnumeric:', s.isnumeric())
print('isascii:', s.isascii())
# s = -1.23
# isalnum: False
# isalpha: False
# isdecimal: False
# isdigit: False
# isnumeric: False
# isascii: True
Strings can be converted to floating point numbers with float(). Error for strings that cannot be converted.
print(float('-1.23'))
# -1.23
print(type(float('-1.23')))
# <class 'float'>
# print(float('abc'))
# ValueError: could not convert string to float: 'abc'
With exception handling, a function can be defined that returns true when a string can be converted with float().
def is_num(s):
try:
float(s)
except ValueError:
return False
else:
return True
print(is_num('123'))
# True
print(is_num('-1.23'))
# True
print(is_num('+1.23e10'))
# True
print(is_num('abc'))
# False
print(is_num('10,000,000'))
# False
If you want to determine that a comma-separated number is also true, use replace() to remove the comma (replace it with an empty string).
def is_num_delimiter(s):
try:
float(s.replace(',', ''))
except ValueError:
return False
else:
return True
print(is_num_delimiter('10,000,000'))
# True
If you want to support whitespace delimitation, you can use replace() further.
def is_num_delimiter2(s):
try:
float(s.replace(',', '').replace(' ', ''))
except ValueError:
return False
else:
return True
print(is_num_delimiter2('10,000,000'))
# True
print(is_num_delimiter2('10 000 000'))
# True