Skip to content
Advertisement

How can I check if a string represents an int, without using try/except?

Is there any way to tell whether a string represents an integer (e.g., '3', '-17' but not '3.14' or 'asfasfas') Without using a try/except mechanism?

JavaScript

Advertisement

Answer

If you’re really just annoyed at using try/excepts all over the place, please just write a helper function:

JavaScript
JavaScript

It’s going to be WAY more code to exactly cover all the strings that Python considers integers. I say just be pythonic on this one.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement