Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Python determine whether a string contains only numbers

Shulou Source: shulou.com Published: 2022-06-01 07:12:21 09月26日 Update

Editor to share with you Python how to determine whether the string contains only numbers, I believe most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

When using Python for data processing, it is often encountered that a column in DataFrame should be a numeric type, but the non-numeric type is mixed in the field because of the irregularity of the data. For this type of data, it is generally necessary to determine whether each value in the column contains only numbers. The common judgment methods are as follows:

Str.isdecimal () function

Function: returns True if all characters in the string are decimal characters and the string has at least one character, otherwise returns False. Decimal characters refer to the characters that can be used to make up decimal numbers, such as Ubun0660, the Arabic alphanumeric 0.

Str.isdigit () function

Function: if all characters in the string are numeric and there is at least one character, return True, otherwise return False. Numbers include decimal characters and numbers that require special processing, such as compatibility superscript digits. This includes numbers that cannot be used to make up decimal numbers, such as Kharosthi numbers. Strictly speaking, a number is a character whose attribute value is Numeric_Type=Digit or Numeric_Type=Decimal.

Str.isnumeric () function

Returns True if there is at least one character in the string and all characters are numeric characters, otherwise returns False. Numeric characters include numeric characters, as well as all characters with numeric properties set in Unicode, such as Utility 2155, VULGAR FRACTION ONE FIFTH. Formally defined as: numeric characters are characters with characteristic attribute values of Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

Example:

In addition, these three functions do not recognize the character ".", so none of the three functions can determine the string converted by a floating-point number (this requires the use of regular expressions).

The above is all the contents of the article "how Python determines whether a string contains only numbers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Characters numbers strings numeric values functions decimal rare attributes data article type decimal processing three function content time binary special superscript Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MariaDB Shulou Information Shulou Technology MySQL