YouTip LogoYouTip

Python3 Check Is Number

# Python3.x Python Check if a String is a Number [![Image 3: Document Object Reference](#) Python3 Examples](#) The following example demonstrates how to check if a string is a number by creating a custom function **is_number()**: ## Example (Python 3.0+) # -*- coding: UTF-8 -*-# Filename : test.py# author by : www..com def is_number(s): try: float(s)return True except ValueError: pass try: import unicodedata unicodedata.numeric(s)return True except(TypeError, ValueError): pass return False# Test strings and numbers print(is_number('foo'))# False print(is_number('1'))# True print(is_number('1.3'))# True print(is_number('-1.37'))# True print(is_number('1e3'))# True# Test Unicode# Arabic 5 print(is_number('Ω₯'))# True# Thai 2 print(is_number('ΰΉ’'))# True# Chinese number print(is_number('Four'))# True# Copyright symbol print(is_number('Β©'))# False We can also use an embedded if statement to achieve this: Executing the above code produces the following output: FalseTrueTrueTrueTrueTrueTrueTrueFalse ### More Methods Python [isdigit()](#) method checks if all characters in the string are digits. Python [isnumeric()](#) method checks if all characters in the string are numeric. This method works only for unicode objects. [![Image 4: Document Object Reference](#) Python3 Examples](#)
← Android Hello World ExampleAndroid Architecture β†’