.isdigit() .isnumeric() .isdecimal()

which of the following will work for a negative number to?

.isnumaric()
.isdigit()
.isdecimal()

thanks a lot!

my program seems to be taking the - sign as a separate value not as part of the number

Then i would certainly advise try .. except, that is then certainly the easiest way to go.

Bear in mind that those are all string methods. That means we have all the manipulative methods of str at our disposal…

>>> ('-42'.strip('-')).isnumeric()
True
>>> ('42'.strip('-')).isnumeric()
True
>>> 

Not any of them, really. It’s text. It’s not a number.

Note that there’s no: str.islistofint … why is that?

Those string methods are not there to tell you whether any python data types match that text, they’re there to inspect the properties of the text.
That might sound silly if you’re thinking about text as ascii only but that’s not what it is. There are lots of unicode symbols representing numbers. is ½ a number? (it is, it’s 0.5, obviously) Is it a digit? In a base 0.5 number system perhaps, yeah, but unicode might not describe it as such. Is it a decimal digit?

You could toss the text to the int type and see if you get something back, or you could pattern match it with regex:

^[+-]?[0-9]+$

^ start of input
$ end of input
? zero or one (optional)
+ one or more
* zero or more
[] any of the things in there, can contain ranges like a-z

You could also write your own logic for inspecting the string, you would for example test whether the first character (if it exists) is + or - (or neither) and then whether the rest is only one of 0…9

Don’t expect finished solutions to exist. Whatever it is, break it apart into smaller things until you reach a point where you know something that solves it, and compose those parts (write code) to produce the overall action you’re looking for.

1 Like

Be careful with the spelling of the name of the method.

See str.isnumeric().