Python check for numeric value

I have a form value:

anytext = request.POST.get("my_text", None)
if anytext and anytext.is_string:
    perform any action
if anytext and anytext.is_numeric:
    perform another action

      

How to check a numeric value

+3


source to share


2 answers


You can use isdigit()

if anytext

always has a type string

.

For example:

'Hello World'.isdigit()  # returns False
'1234243'.isdigit()  # returns True

      



So with your code:

anytext = request.POST.get("my_text", "")
if anytext.isdigit():
    # perform action on numeric string
else:
    # perform action on alphanumeric string

      

+2


source


You may try:



 if int(re.search(r'\d+', anytext).group())):
    #perform action

      

0


source







All Articles