varad
varad

Reputation: 8029

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 can I check for numeric value ??

Upvotes: 2

Views: 835

Answers (2)

Shruti Srivastava
Shruti Srivastava

Reputation: 398

You can try :

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

Upvotes: 0

Martin Konecny
Martin Konecny

Reputation: 59611

You could use isdigit() assuming anytext is always of 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

Upvotes: 2

Related Questions