Home > Blockchain >  how to know if a variable is not containing a number in python?
how to know if a variable is not containing a number in python?

Time:03-05

so I know how to check if a variable contains numbers. like :

variable.isdigit

but how to know if its not containing a number and it have letters or symbols or... in it.

CodePudding user response:

The easiest to find out about the type of the variable is to use type:

>>> x=1.23
>>> y=3
>>> z='blah'
>>> print(type(x), type(y), type(z))
<class 'float'> <class 'int'> <class 'str'>

If you need boolean tests, you have a bunch of other var.is* options:

x.isalnum()       x.isdecimal()     x.islower()       x.isspace()
x.isalpha()       x.isdigit()       x.isnumeric()     x.istitle()
x.isascii()       x.isidentifier()  x.isprintable()   x.isupper()

CodePudding user response:

Please check the documentation, e.g. https://www.w3schools.com/python/python_ref_string.asp

  • the opposite of .isdigit(): not variable.isdigit()
  • Related