Implement IsNumber() function in Python Last Updated : 06 May, 2021 Comments Improve Suggest changes 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 id() function in Python In Python, id() function is a built-in function that returns the unique identifier of an object. The identifier is an integer, which represents the memory address of the object. The id() function is commonly used to check if two variables or objects refer to the same memory location. Python id() Fun 3 min read 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 input() Function Python input() function is used to take user input. By default, it returns the user input in form of a string.input() Function Syntax: input(prompt)prompt [optional]: any string value to display as input messageEx: input("What is your name? ")Returns: Return a string value as input by the user.By de 4 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 Array in Python | Set 2 (Important Functions) Array in Python | Set 1 (Introduction and Functions)Array in Python | Set 2Below are some more useful functions provided in Python for arrays: Array Typecode FunctionThis function returns the data type by which the array is initialized. In this example, we are using arr.typecode to find out the data 3 min read Python oct() Function Python oct() function takes an integer and returns the octal representation in a string format. In this article, we will see how we can convert an integer to an octal in Python. Python oct() Function SyntaxSyntax : oct(x) Parameters: x - Must be an integer number and can be in either binary, decimal 2 min read Python Bin | Count total bits in a number Given a positive number n, count total bit in it. Examples: Input : 13 Output : 4 Binary representation of 13 is 1101 Input : 183 Output : 8 Input : 4096 Output : 13 We have existing solution for this problem please refer Count total bits in a number link. Approach#1: We can solve this problem quick 3 min read Like