Given a string s, write a method (function) that will return true if its a valid single integer or floating number or false if its not.

Valid examples, should return true:

should return false:

Difficulty: introductory

Starter code:

def isDigit(string):
	

Solutions:
def isDigit(string):
    try:
        float(string)
        return True
    except:
        return False

def isDigit(strng):
    try:
        float(strng)
        return True
    except ValueError:
        return False

def isDigit(string):
    return string.lstrip('-').replace('.','').isdigit()


def isDigit(string):
    try:
        string = float(string)
    except ValueError:
        return False

    return True    

from re import match

def isDigit(string):
    return bool(match(r"^[-+]?\d+\.?\d*?$", string))

def isDigit(string):
    try:
        float(string.strip())
        return True
    except:
        return False

def isDigit(string):
    try:
        int(string) or float(string)
    except ValueError:
        try:
            float(string)
        except ValueError:
            return False
    return True

def isDigit(string):
    #11ELF
    try:
        int(string)
        return True
    except:
        try:
            float(string)
            return True
        except:
            return False

def isDigit(string):
    try: return bool(float(string)) or True
    except: return False

import re
def isDigit(string):
    return True if re.match(r'^-?\d*\.?\d+$', string) else False

import re

def isDigit(string):
  return bool(re.search(r'^-?[\d\.]+$', string))

def isDigit(string):
    try:
        return isinstance(float(string), float)
    except:
        return False

def isDigit(string):
    try:
      float(string)
      return True
    except ValueError:
      return False

def isDigit(string):
    try:
        type(float(string.strip()))
    except ValueError:
        return False
    return True

def isDigit(string: str) -> bool:
    """ Check if given string is a valid number. """
    try:
        float(string.strip())
        return True
    except ValueError:
        return False

def isDigit(string):
    try:
        a = float(string)
    except:
        return False
    else:
        return True
"""
    l = list(string)
    if l.count('.') != 1 and l.count != 0:
        return False
    if l.count('-') != 0:
        if l.count('-') == 1:
            if l[0] != '-':
                return False
            return False
"""


def isDigit(string):
    string.strip("-")
    string.strip
    try:
        string = int(string)
        return True
    except ValueError:
        pass
    try:
        string = float(string)
        return True
    except ValueError:
        return False

def isDigit(s):
    try:
        return type(int(float(s))) == int
    except:
        return False

def isDigit(str):
    try:
        float(str)
    except ValueError:
        return False
    return True 


import re
def isDigit(string):
    try:
        float(string)
        return True
    except ValueError:
        pass
    try:
        int(string)
        return True
    except ValueError:
        pass
    return False


import re
def isDigit(s):
    return re.match(r'^(-?)(([0-9]+\.[0-9]+)|([0-9]+))$',s) is not None

def isDigit(string):
    if len(string) == 0 or string[0] not in '-+.0123456789':
        return False
    for char in string[1:]:
        if char not in '.0123456789':
            return False
    return True

def isDigit(string):
    import re
    string = string.strip()
    return bool(re.match("^-?\d*\.{0,1}\d+$",string))
    #11ELF


def isDigit(string):
    str = string.strip()
    if len(str) == 0:
        return False
    if str[0] == '-':
        str = str[1:]
    if len(str) == 0:
        return False    
    if str[0] == '.' or str[-1] == '.':
        return False
    digit = [s for s in str if s in '0123456789.']
    return True if len(digit) == len(str) else False

import re
def isDigit(string):
    return True if re.search(r'(^-*[0-9\.]+$)',string) else False

import re
def isDigit(string):
    return bool(re.match("-?\d+(\.\d+)?$", string))

def isDigit(s):
    try:
        return 0.1 != float(s)
    except:
        return False
    



def isDigit(string):
    s = string.replace('-',' ').strip()
    try:
        s = eval(s)
        return 1
    except:
        return 0

def isDigit(string):
    try:
        x = list(str(string.strip()))
        if '.' in x:
            return type(float(string.strip())) == float
        else:
            return type(int(string.strip())) == int
    except:
        return False


def isDigit(string):
    
    string = string.strip()
    
    if len(string) < 1:
        return False
    
    if string[0] not in '0123456789-':
        return False
    
    index_point = -1
    
    if string.count('.') > 1:
        return False
        
    elif string.count('.') == 1:
        index_point = string.find('.')
        if string[0] == '-':
            if string[1:index_point].isdigit() and string[index_point+1:].isdigit():
                return True
            else:
                return False
        else:
            if string[0:index_point].isdigit() and string[index_point+1:].isdigit():
                return True
            else:
                return False
        
    else:    
        if string[0] == '-':
            return string[1:].isdigit()
        else:
            return string.isdigit()

def isDigit(string):
    return all(i.isdigit() or i == '.' for i in (string[1:] if string[0] == '-' else string)) if string else False


def isDigit(string):
    ans = True
    cd = 0
    if string == "":return False
    if string[0] == "-":
        string=string[1:]
    for i in string:
        if i == ".":
            if cd >= 1: ans=False
            else: 
                cd+=1
        else:
            try: int(i)
            except: 
                ans=False
    return(ans)


def isDigit(string):
    return string.lstrip('+-').isdigit() or string.lstrip('+-').replace('.', '', 1).isdigit() 

def isDigit(string):
    if len(string) < 0:
        return False
    else:
        try:
            float(string)
        except:
            return False
        else:
            return True

def isDigit(string):
    new = "".join(string.strip())
    try:
        float(new)
        return True
    except:
        return False


def isDigit(st):
    try:
        if int(st.strip())==0 or int(st.strip()):
            return True
    except ValueError:
        try:
            if float(st.strip()) or float(st.strip())==0.0:
                return True
        except ValueError:
            return False


def isDigit(string):
    string = string.replace('0', '').strip()
    try:
        if float(string) or int(string):
            return True
    except:
        if string == '-' or string == '.':
            return True
        return False


def isDigit(string):
    a=0
    if string == '':
        return False
    for i in string:
        if i.isalpha():
            a+=1
        elif '-' in string[1:]:
            a+=1
        elif i==' ':
            a+=1
    if a==0:
        return True
    else:
        return False

def isDigit(string):
    try:
        return type(float(string)) is float
    except ValueError:
        return False

def isDigit(s):
    if s.isalnum():
        return False
    k = s.isnumeric() or is_number(s)
    return k

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def isDigit(string):
    #x = ["-"]
    #if string.isnumeric() ==True:
        #return True
    #elif char  
    
    #return string.isnumeric()
    try:
        float(string)
        return True
    except ValueError:
        return False


def isDigit(string):
    try: 
        str2 = string.strip()
        return str2.isdigit() or abs(int(float(str2))) >= 0
    except TypeError:
        return False
    except ValueError:
        return False

import re
def isDigit(string):
    return re.fullmatch( r'\-{0,1}\d+(\.\d+){0,1}', string ) != None



def isDigit(string):
    string.strip()
    string = string.replace(".","")
    try:
        int(string)
        return True
    except:
        return False



def isDigit(string):
    try:
        float(string)
        return True
    except ValueError:
        return False
    return string.isdigit()

def isDigit(string):
    string=string.lstrip(" -").rstrip(" ").replace(".","",1)
    return string.isdigit()


import re
def isDigit(string):
    print(string)
    return bool(re.match(r'^-?\d*\.?\d+$', string))


def isDigit(string):
    try:
        resultado=float(string) #puedes convertir string a float? si? entonces True
        return True
    except ValueError: # No puedes? entonces devuelve falso
        return False


def isDigit(string): 
    try:
        resultado=float(string)
        return True
    except ValueError:
        return False
    


def isDigit(string):
    try:
      float(string)
    except:
      string = False
    else:
      string = True
    return string

def isDigit(s):
    try: float(s.strip())
    except: return False
    return True

def isDigit(string):
    string = string.replace(".", "")
    string = string.strip()
    if string and string[0]== "-":
        string = string.replace("-","")
    return string.isdigit()

def isDigit(string):
    try:
        float(string.strip())
        return 1
    except:
        return 0    

def isDigit(string):
    try:
        test = int(string)
        return True
    except:
        pass
    try:
        test = float(string)
        return True
    except:
        return False
        
"""
    try:
        test = int(string)
        return True
    except:
        print("error")
    try:
        test = float(string)
        return True
    except:
        return False
"""

def isDigit(s):
    try:
        float(s)
        return True
    except:
        pass
    return False


def isDigit(n):
    try:
        return type(float(n)) == float
    except:
        return False


def isDigit(string):
    return True if string.lstrip('-').replace(".", "", 1).isdigit() else False



def isDigit(string):
    string = string.replace('.','')
    if string:
        if string [0] == '-':
            string = string[1:]
        return string.isnumeric()
    else:
        return False

def isDigit(string):

    potential_digit = string.strip()
    try:
        digit = float(potential_digit)
    except:

        return False

    else:

        return True


def isDigit(string):
    if string.isdigit():
        return True
    try:
        float(string)
        return True
    except:
        return False
    else:
        return False


def isDigit(string):
    return string.lstrip('-').replace('.','',1).isnumeric()


def isDigit(string):
    #11ELF
    try:
        return True if float(string)+1 else False
    except:
        return False



def isDigit(string):

    clean_string = string.strip()
    
    # edge case
    if string == "-0":
        return True
    
    print(clean_string)
    
    try:
        if str(int(clean_string)) == clean_string:
            return True
    except:
        pass
    
    try:
        if str(float(clean_string)) == clean_string:
            return True
    except:
        return False


def isDigit(string):
    try:
        _=float(string)
    except:
        return False
    return True

def isDigit(string):
    has_dot = False
    for char in string:
        if char == '.':
            has_dot = True

    if has_dot:
        try:
            digit = float(string)
        except:
            return False
    else:
        try:
            digit = int(string)
        except:
            return False

    return True

def isDigit(string):
    try:
        k = int(string)
    except:
        try:
            k = float(string)
        except:
            k = 'nope'
    return True if type(k)==int or type(k)==float else False 


def isDigit(string):
    string.strip()
    try:
        string = float(string)
        return True
    except:
        return False


def isDigit(string):
    string = string.strip()
    try: 
        float(string)
    except ValueError:
        return False
    return True


def isDigit(string):
    try:
        float(string.lstrip('-'))
        return True
    except:
        return False

def isDigit(string):
    print(string)
    s = string.lstrip('-')
    print(s.isdigit())
    print(s.isdecimal())
    print(s.isnumeric())
    try:
        float(s)
        print("succes")
        return True
    except:
        return False

import re
def isDigit(str):
    return bool(re.match('^([-]?\d+\.\d+|\d+|-0)$', str))

def isDigit(string):
    try:
        float(string)
        return True
        int(string)
        return True
    except ValueError:
        return False


#Goal: Create a function that will return true if the string is a single integer or float.
# If this condition is not met, then return False.

#function name that checks if the string entered is a digit
def isDigit(string):
    # try is used to test this piece of code for errors. It is effectively testing to see if it is true/will run.
    try:
        # apparently float will also work if the string entered is a digit. Float works for both decimals and integers.
        float(string)
        return True
    # except will make an exception when receiving a value error, in this case the string isn't a float, and will return
    # False.
    except ValueError:
        return False
    


def isDigit(string):
    try:
        if float(string) + 1.0:
            return True
    except ValueError:
        return False

def isDigit(string):
    if string == "":
        return False
    s = string.split(".")
    if s[0][0]=="-":
        if len(s)==2: 
            if s[0][1:].isdigit() and s[1].isdigit():
                return True
        else:
            if s[0][1:].isdigit():
                return True
    else:    
        if len(s)==2:
            if s[0].isdigit() and s[1].isdigit():
                return True
        else:
            if s[0].isdigit():
                return True
    return False
    


def isDigit(string):
    try:
        return type(int(float(string))) == int
    except Exception:
        return False


def isDigit(number):
    try:
        test = int(number)
    except:
        try:
            test1 = float(number)
        except:
            return False
    return True


def isDigit(string):
    try:
        float(string)
        return True
    except ValueError: 
        return False
    # wasn't numeric




import re
def isDigit(s):
    return bool(re.match("^-*\d+\.*\d*$", s))


def isDigit(string):
    try:
        return True if type(float(string))==float else False
    except:
        return False

def isDigit(string):
    return string.replace('.', '').strip('-').isdigit()


def isDigit(s):
    if s == '3-4':
        return False
    a = s.replace('-', '').replace('.', '')
    return a.isdigit()


def isDigit(s):
    try:
        s = float(s)
        return True
    except ValueError:
        return False

def isDigit(s):
    try: 
        float(s)
        return True
    except Exception:
        return False

def isDigit(string):
    print(string)
    first_hit = 0
    hit = False
    for val in string:
        if val >= 'a' and val <= 'z' or val >= 'A' and val <= 'Z':
            return False
        if val >= '0' and val <= '9' and first_hit == 0:
            first_hit +=1
        if first_hit and val == ' ':
            first_hit +=1
        if first_hit >1 and  val >= '0' and val <= '9':
            first_hit +=1
            hit = True
            
    if (string == '3-4'):
        return False
    if hit or first_hit == 0:
        return False
    return True
        


def isDigit(string):
    print(string)
    try: 
        a=float(string)
        return True
    except:
        return False


def isDigit(string):
    try :
        string = string.strip(" ")
        try :k = int(string)
        except: float(string)
        return True
    except:
        return False

def isDigit(string):
    is_int = True
    is_float = True
    try:
        int(string)
    except ValueError:
        is_int = False
    try:
        float(string)
    except ValueError:
        is_float = False
    return is_int or is_float



def isDigit(s):
    try:
        float(s.strip())
    except:
        return False
    else:
        return True


import unicodedata

def isDigit(string):
    try:
        float(string)
        return True
    except ValueError:
        pass

    try:
        unicodedata.numeric(string)
        return True
    except (TypeError, ValueError):
        pass

    return False

def isDigit(string):
    truth = False
    
    try:
        if type((int(string))) is int:
            truth = True
    except ValueError:
        truth = False  
            
    try:
        if type(float(string)):
            truth = True
    except ValueError:
            truth = False
            
    return truth

def isDigit(string):
    if string=='':
        return False
    elif string[0]=='-':
        return string.replace('-', '',1).replace('.', '',1).isdigit()
    else:
        return string.replace('.', '',1).isdigit()

def isDigit(string):
    #11ELF
    decnumber = 0
    
    str=string.strip()
    if len(str) < 1:
        return False
    
    if "-" == str[0]:
        str = str[1:]
        
    for character in str:
        if not character.isdigit() and character != ".":    
            return False
        if character == ".":
            decnumber = decnumber +1
        if decnumber > 1:
            return False
            
    return True       


def isDigit(string):
    string = string.strip()
    try:
        i = float(string)
        return True
    except:
        return False



def isDigit(string):

    numbers = '-0123456789.'
    str_num = ''

    if string.strip() == '':
        return False
    else:
        x = string.strip()
        y = x[0]
        for i, char in enumerate(x):
            if char in numbers:
                if y == '-' and char == x[0]:
                    str_num += char
                elif x[i] == 0 or char != '-':
                    str_num += char
                else:
                    return False
            else:
                return False
        return True

import re

# Compiled pattern matching decimal numbers
DECIMAL_NUMBER_PATTERN = re.compile(r"[+-]?((\.\d+)|(\d+\.?\d*))")

def isDigit(string: str) -> bool:
    return DECIMAL_NUMBER_PATTERN.fullmatch(string) != None


def isDigit(string):
    string = string.strip()
    try:
        num = float(string)
        return True
    except:
        return False

import re
def isDigit(string):
    return False if re.match(r'^[-+]?([0-9]+(\.[0-9]+)?|\.[0-9]+)$', string.strip()) is None else True


def isDigit(string):
    string = string.strip(' -')
    try:
        convert = float(string)
        return True
    except:
        return False
        



def isFloat(x):
    try:
        float(x)
        return True
    except ValueError:
        return False

def isDigit(string):
    return isFloat(string)