-2

I know the string methods str.isdigit, str.isdecimal and str.isnumeric.

I'm looking for a built-in method that checks if a character is algebraic, meaning that it can be found in a declaration of a decimal number.

The above mentioned methods return False for '-1' and '1.0'.

I can use isdigit to retrieve a positive integer from a string:

string = 'number=123'
number = ''.join([d for d in string if d.isdigit()])  # returns '123'

But that doesn't work for negative integers or floats.

Imagine a method called isnumber that works like this:

def isnumber(s):
    for c in s:
        if c not in list('.+-0123456789'):
            return False
    return True

string1 = 'number=-1'
string2 = 'number=0.1'
number1 = ''.join([d for d in string1 if d.isnumber()])  # returns '-1'
number2 = ''.join([d for d in string2 if d.isnumber()])  # returns '0.1'

The idea is to test against a set of "basic" algebraic characters. The string does not have to contain a valid Python number. It could also be an IP address like 255.255.0.1. . Does a handy built-in that works approximately like that exist?

If not, why not? It would be much more efficient than a python function and very useful. I've seen alot of examples on stackoverflow that use str.isdigit() to retrieve a positive integer from a string. Is there a reason why there isn't a built-in like that, although there are three different methods that do almost the same thing?

uzumaki
  • 1,743
  • 17
  • 32

1 Answers1

-1

No such function exists. There are a bunch of odd characters that can be part of number literals in Python, such as o, x and b in the prefix of integers of non-decimal bases, and e to introduce the exponential part of a float. I think those plus the hex digits (0-9 and A-F) and sign characters and the decimal point are all you need.

You can put together a string with the right character yourself and test against it:

from string import hex_digits

num_literal_chars = hex_digits + "oxOX.+-"

That will get a bunch of garbage though if you use it to test against mixed text and numbers:

string1 = "foo. bar. 0xDEADBEEF 10.0.0.1"
print("".join(c for c in string1 if c in num_literal_chars))
# prints "foo.ba.0xDEADBEEF10.0.0.1"

The fact that it gives you a bunch of junk is probably why no builtin function exists to do this. If you want to match a certain kind of number out of a string, write an appropriate regular expression to match that specific kind of number. Don't try to do it character-by-character, or try to match all the different kinds of Python numbers.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • All right, I've edited my answer. Basically, matching all possible "number characters" is going to give you junk a lot of the time. Unlike digits that don't often appear in non-numeric contexts, other bits like periods and dashes do occur all the time in other kinds of text. – Blckknght May 29 '16 at 02:20
  • In the example I gave in my question, with only adding the characters '.+-' to a set of digits to support decimals, you will not get junk if you know what you're doing. Also, this is not even relevant, because we're talking about a category of characters. And testing against a set of "classic" algebraic characters like '+-*/.0123456789' could be an absolutely legit string method. Whether it returns junk or not is up to the implementation. – uzumaki May 29 '16 at 19:31