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")
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
.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
integer - Python's isdigit() method returns False for negative numbers - Stack Overflow
I have a text files that I read to a list. This list contains integers and strings. For example, my list could look like this: ["name", "test", "1", "3", "-3", "name" ...] Now, I want to convert ... More on stackoverflow.com
🌐 stackoverflow.com
Can someone help me with this problem?
You have to remove: if top_of_range.isdigit(): top_of_range = int(top_of_range) And write: top_of_range = int(input("Type a number: ")) Edit: You take the first input number as a string, then the code checks if it is an integer, if it is an integer, it will then cast the integer into an integer - which doesn't make much sense. You could change it to check if it is a string instead though, if you want to keep those lines. The code have a bunch of other oddities, like the quit statements after every if-else, why are those there? More on reddit.com
🌐 r/learnpython
8
1
December 26, 2022
🌐
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....
🌐
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 ...
🌐
Miguendes
miguendes.me › python-isdigit-isnumeric-isdecimal
How to Choose Between isdigit(), isdecimal() and isnumeric() in Python
September 18, 2021 - 4.2. How to Check if Negative Numbers Are Digits? 4.3. Why isdigit Is Not Working for Me? Conclusion · str.isdigit() is the most obvious choice if you want to determine if a string - or a character - is a digit in Python. According to its documentation, this method returns True if all characters in the string are digits and it's not empty, otherwise it will return False. Let's see some examples: # all characters in the string are digits >>> '102030'.isdigit() True # 'a' is not a digit >>> '102030a'.isdigit() False # isdigit fails if there's whitespace >>> ' 102030'.isdigit() False # it must be at least one char long >>> ''.isdigit() False # dots '.' are also not digit >>> '12.5'.isdigit() False ·
🌐
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 - The isdigit() method is a Python ... and mathematical symbols—including the negative sign—automatically cause it to return False. For example, "123".isdigit() ......
🌐
freeCodeCamp
forum.freecodecamp.org › python
Beginner Python question - Python - The freeCodeCamp Forum
June 13, 2023 - 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 number would return as false, it defaults to the else statement that says to type a number next time. is this an oversight since a negative number can’t get the program to generate “please type a number larger...
🌐
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 ...
January 26, 2017 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
Find elsewhere
🌐
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 to code own function for negative checks or avoid negative number using abs() that is absolute value of number even if input is with - ... PyCharm. Feel the difference in data science and AI/ML workflows. The only Python IDE you need to build data models and AI agents.
🌐
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!
🌐
EyeHunts
tutorial.eyehunts.com › home › python isdigit negative
Python isdigit negative - Tutorial - By EyeHunts
February 21, 2023 - 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.
🌐
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.
🌐
Tutorial Gateway
tutorialgateway.org › python-isdigit
Python isdigit
March 25, 2025 - Str1 = '123456789'; print('First ... = '1234.67'.isdigit() print('Fourth Output = ', Str6) # Performing on Negative Values Str7 = '-1234'.isdigit() print('Fifth Output = ', Str7)...
🌐
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 - Because of this, it will only return True if all the characters can evaluate to a base ten number, meaning that fractions and superscripts will return False. Let’s take a look at the same examples as above and see what the isdecimal method returns: # Python isdigit to check if characters are digit values # Check if a string containing an integer is a decimal >>> integer = '2' >>> print(f'{integer.isdecimal()=}') integer.isdecimal()=True # Check if a string containing a float is a decimal >>> floats = '2.3' >>> print(f'{floats.isdecimal()=}') floats.isdecimal()=False # Check if a string containing a fraction is a decimal >>> fraction = '⅔' >>> print(f'{fraction.isdecimal()=}') fraction.isdecimal()=False # Check if a string containing an exponent is a decimal >>> exponent = '2²' >>> print(f'{exponent.isdecimal()=}') exponent.isdecimal()=False
🌐
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 ...
🌐
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?
🌐
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