Use lstrip:

question.lstrip("-").isdigit()

Example:

>>>'-6'.lstrip('-')
'6'
>>>'-6'.lstrip('-').isdigit()
True

You can lstrip('+-') if you want to consider +6 a valid digit.

But I wouldn't use isdigit, you can try int(question), it'll throw an exception if the value cannot be represented as int:

try:
    int(question)
except ValueError:
    # not int
Answer from Maroun on Stack Overflow
Top answer
1 of 7
88

Use lstrip:

question.lstrip("-").isdigit()

Example:

>>>'-6'.lstrip('-')
'6'
>>>'-6'.lstrip('-').isdigit()
True

You can lstrip('+-') if you want to consider +6 a valid digit.

But I wouldn't use isdigit, you can try int(question), it'll throw an exception if the value cannot be represented as int:

try:
    int(question)
except ValueError:
    # not int
2 of 7
28

Use a try/except, if we cannot cast to an int it will set is_dig to False:

try:
    int(question)
    is_dig = True
except ValueError:
    is_dig = False
if is_dig:
  ......

Or make a function:

def is_digit(n):
    try:
        int(n)
        return True
    except ValueError:
        return  False

if is_digit(question):
   ....

Looking at your edit cast to int at the start,checking if the input is a digit and then casting is pointless, do it in one step:

while a < 10: 
     try:
        question = int(input("What is {} {} {} ?".format(n1,op,n2)))
     except ValueError:
        print("Invalid input")
        continue # if we are here we ask user for input again

    ans = opsop
    n1 = random.randint(1,9)
    n2 = random.randint(1,9)
    op = random.choice(list(ops))

    if question ==  ans:
        print ("Well done")
    else:
        print("Wrong answer")
    a += 1

Not sure what Z is doing at all but Z = Z + 0 is the same as not doing anything to Z at all 1 + 0 == 1

Using a function to take the input we can just use range:

def is_digit(n1,op,n2):
    while True:
        try:
            n = int(input("What is {} {} {} ?".format(n1,op,n2)))
            return n
        except ValueError:
            print("Invalid input")


for _ in range(a):
    question = is_digit(n1,op,n2) # will only return a value when we get legal input
    ans = opsop
    n1 = random.randint(1,9)
    n2 = random.randint(1,9)
    op = random.choice(list(ops))

    if question ==  ans:
        print ("Well done")
    else:
        print("Wrong answer")
🌐
Sololearn
sololearn.com › en › Discuss › 188881 › is-there-a-way-to-use-isdigit-with-strings-that-could-contain-negative-numbers
Is there a way to use .isdigit() with strings that could contain negative numbers? | Sololearn: Learn to code for FREE!
January 26, 2017 - Would the best way to do this be to check for the "-" at the start of the string, strip it and then reapply it after the .isdigit() check? This seems a bit convoluted. ... use lstrip() function first. This will work for positive and negative numbers.
Discussions

Beginner Python question
i’m new to python and i have a question about the numbers guessing game project in this video 5 Mini Python Projects - For Beginners - YouTube. he uses isdigit() to identify whether the user’s input is a number, but isdigit() can’t identify negative numbers if i’m correct? since a negative ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
June 13, 2023
Using regular expressions and filter why does isdigit return false on a number?
It doesn't give back 42, it gives back -42. - is not a digit character, and so isdigit returns False. More on reddit.com
🌐 r/learnprogramming
7
1
June 24, 2022
.isdigit() .isnumeric() .isdecimal()
which of the following will work for a negative number to? .isnumaric() .isdigit() .isdecimal() thanks a lot! More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
December 3, 2019
Handling negative number inputs from the user
I would recommend you use a try instead. answer = input(f"What is {a} + {b}") try: answer = int(answer) except ValueError: print("That's not a number") More on reddit.com
🌐 r/learnpython
19
3
January 29, 2025
🌐
Nextjournal
nextjournal.com › avidrucker › detecting-valid-number-strings-in-python
Detecting Valid Number Strings in Python - Nextjournal
*In other words, learning RegEx's would take substantially more work to learn, because they use an entirely different syntax than Python. ... Python has an "isdigit" function, but, it fails on decimal numbers and negative numbers.
🌐
Quora
quora.com › What-method-checks-that-the-string-is-a-number-and-includes-negative-in-the-Python-language
What method checks that the string is a number and includes negative in the Python language? - Quora
That is simple check to find that typed input is a positive number it will return True if it is False if it is not but and also False if it is negative.Other built in method is isdigit() but also not accept negative number that consent you have ...
🌐
freeCodeCamp
forum.freecodecamp.org › python
Beginner Python question - Python - The freeCodeCamp Forum
June 13, 2023 - i’m new to python and i have ... but isdigit() can’t identify negative numbers if i’m correct? since a negative number would return as false, it defaults to the else statement that says to type a number next time. is ...
🌐
Devcuriosity
devcuriosity.com › manual › details › python-isdigit-function
Python - isdigit() function with examples. Check string for digits.
bytearray_1 = bytearray(b"12345") ... characters (0-9) and returns True if all characters are digits. Case Sensitivity: Alphabetic characters, decimal points, and negative signs result in False....
Find elsewhere
🌐
Miguendes
miguendes.me › python-isdigit-isnumeric-isdecimal
How to Choose Between isdigit(), isdecimal() and isnumeric() in Python
September 18, 2021 - Use str.isdigit when you want to verify that each and every character in a string is a single digit, that is, not punctuation, not a letter, and not negative. The str.isdecimal() method is very similar, it returns True if all chars are decimal ...
🌐
Codecademy Forums
discuss.codecademy.com › get help › python
.isdigit() .isnumeric() .isdecimal() - Python - Codecademy Forums
December 3, 2019 - which of the following will work for a negative number to? .isnumaric() .isdigit() .isdecimal() thanks a lot!
🌐
Netjstech
netjstech.com › 2019 › 08 › python-string-isdigit-method.html
Python String isdigit() Method | Tech Tutorials
October 4, 2025 - 2. Using isdigit() method with superscripts or subscripts. For such strings, which uses unicode characters, isdigit() method returns true. str = '2\u2076' print(str) print(str.isdigit()) str = '4\u2082' print(str) print(str.isdigit()) ... 3. Using isdigit() method with negative numbers or decimal numbers.
🌐
Reddit
reddit.com › r/learnpython › handling negative number inputs from the user
r/learnpython on Reddit: Handling negative number inputs from the user
January 29, 2025 -

This is a solution post. I had a problem and none of the solutions I found online were right for me. I eventually figured it out, and so I'm putting my solution here for future learners. Also if my solution is bad, I'll get some feedback. If you think it's obvious, then you're very clever, but no need to go to the trouble of letting me know!

I'm making an arithmetic game for my little one. So it had a line:

answer = int(input(f"What is {a} + {b}?"))

but of course he accidentally typed a letter and crashed the program. I wanted to handle this eventuality so I changed it to:

answer = input(f"What is {a} + {b}")
if answer.isnumeric():
    answer = int(answer)
else:
    print("That's not a number")
    continue

the trouble is I also have subtraction questions and negative numbers! But "-1".isnumeric()==False !!

So I started googling: "isnumeric negative numbers" and "parsing negative numbers" and so on. The solutions I found were quite convoluted, mostly they seemed to be worrying about SQL injection and used concepts I hadn't learned yet. I wanted a solution that only used the beginner stuff I already knew. I realised that I only had to check if the first symbol is "-" and the rest is numeric. So:

if answer.isnumeric() or answer[0]=="-" and answer[1:].isnumeric():

is the solution!

EDIT: There was a typo in my solution, I meant to check if everything after the initial "-" is numeric. Otherwise an answer like "-3e" gets through. Thanks to u/Rizzityrekt28 for the catch

🌐
CopyProgramming
copyprogramming.com › howto › how-to-type-negative-number-with-isdigit
How to Type Negative Number with isdigit: Complete 2026 Guide to Python String Validation
December 18, 2025 - Python's isdigit() method is a built-in string validator that checks if all characters in a string are numeric digits (0-9). However, this method returns False for negative numbers because the minus sign (-) is not considered a digit. This limitation creates a common validation challenge for ...
🌐
Reddit
reddit.com › r/learnpython › can someone help me with this problem?
r/learnpython on Reddit: Can someone help me with this problem?
December 26, 2022 - If you want to avoid the user entering a negative number, and don't want to deal with it, you could make use of abs() to make the value absolute, but I kind of understand that the whole point is to print the error if the number is negative. But you still have to remove the last quit() after else, otherwise it doesn't matter what you do, the program will quit even if the number entered is legal. The second part is going to need some work too but I think you're right on path! More replies More replies ... This is a guess, as I’ve never used “isdigit” before, but could it be that it is not working because “-“ in your negative number is not a digit?
🌐
EyeHunts
tutorial.eyehunts.com › home › python isdigit negative
Python isdigit negative - Tutorial - By EyeHunts
February 21, 2023 - Use lstrip() function first. This will work for positive and negative numbers. str = "-2137" res = str.lstrip('-').isdigit() print(res) ... Do comment if you have any doubts or suggestions on this Python isdigit topic.
🌐
CSDN
devpress.csdn.net › python › 630454917e66823466199f6f.html
How to type negative number with .isdigit?_python_Mangs-Python
August 23, 2022 - while a <=10 + Z: question = input("What is " + str(n1) + str(op) + str(n2) + "?") a = a+1 if question.lstrip("-").isdigit() is True: ans = ops[op](n1, n2) n1 = random.randint(1,9) n2 = random.randint(1,9) op = random.choice(list(ops)) if int(question) is ans: count = count + 1 Z = Z + 0 print ("Well done") else: count = count + 0 Z = Z + 0 print ("WRONG") else: count = count + 0 Z = Z + 1 print ("Please type in the number")
🌐
datagy
datagy.io › home › python posts › python strings › choose between python isdigit(), isnumeric(), and isdecimal()
Choose Between Python isdigit(), isnumeric(), and isdecimal() • datagy
December 19, 2022 - Learn how to use the Python isdigit, isnumeric, and isdecimal methods to check whether or not a string is a number and their differences.
🌐
Stack Overflow
stackoverflow.com › questions › 74504679 › how-to-distinguish-negative-numbers-from-input-that-is-not-a-number
python - How to distinguish negative numbers from input that is not a number - Stack Overflow
The reason it's returning "It is not a number" for negative numbers is because user_guess.isdigit() treats negative numbers as strings (or non-digits).