Implement IsNumber() function in Python Last Updated : 06 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article we will see how to implement isNumber() method using Python. This method takes in a string as input and returns True or False according to whether the string is a number or not. Examples: Input : "12345" Output : True Input : "-12345" Output : True Input : "abc" Output : False Approach : Here we will take advantage of the int() built-in function available in Python. Also with this approach we will see how Exception Handling comes to our aid. Using try-catch construct we try to convert the string to integer. In case the string cannot be converted the handler catches the exception that is thrown.Below is the Implementation: Python3 # Implementation of isNumber() function def isNumber(s): # handle for negative values negative = False if(s[0] =='-'): negative = True if negative == True: s = s[1:] # try to convert the string to int try: n = int(s) return True # catch exception if cannot be converted except ValueError: return False s1 = "9748513" s2 = "-9748513" s3 = "GeeksforGeeks" print("For input '{}' isNumber() returned : {}".format(s1, isNumber(s1))) print("For input '{}' isNumber() returned : {}".format(s2, isNumber(s2))) print("For input '{}' isNumber() returned : {}".format(s3, isNumber(s3))) Output : For input '9748513' isNumber() returned : True For input '-9748513' isNumber() returned : True For input 'GeeksforGeeks' isNumber() returned : False Note : This is not the only way to implement the isNumber() function but it is arguably the fastest way of doing so. Try/Catch doesn't introduce much overhead because the most common exception is caught without an extensive search of stack frames. Comment More infoAdvertise with us Next Article Implement IsNumber() function in Python K Kaustav kumar Chanda Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python int() Function The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.Example:In this example, we passed a string as an argument to the int() function and printed it.Pythonage = "21" print("age =", int(age) 3 min read hex() function in Python hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16. Example:Pythona = 255 res = hex(a) print(res)Output0xff Explanat 2 min read Python len() Function The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G 2 min read Python 3 - input() function In Python, we use the input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function converts it into a string.Python input() Function SyntaxSyntax: input(prompt)Parameter:Prompt: (optional 3 min read Format a Number Width in Python Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python. Format a Numbe 3 min read Like