Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (19.9k points)

It's kind of difficult to explain the problem but I'll try my best. I'm writing a program in which I'm transferring info via strings(Which may be representative of an integer just in a string form). These strings either are representative of an integer or string. My problem is when I need to figure out which of the above is the case, I can't find the right way to do it.

I've tried type and it doesn't work because the type is always a string and I can't use int because when I try to use Type Conversion if the string is representative of a string there will be an exception.

entry = 'ss'

if type(int(entry)) is int:

    print('this returns an exception')

1 Answer

0 votes
by (25.1k points)

As int(entry) will either return an int or raise an exception you do not need to check the type, what you need is a try statement, like this:

entry = 'ss'



entry = int(entry)

except ValueError:

Browse Categories