.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.