I'm writing a Python program to convert a set of input strings into integers, then print them out. The input strings do not need to be integers (for example, they can be "udai2"), in which case, I want to print out that the string is not valid.
My original though was to simply convert each string to an integer using the int
function, like int(input_str)
. Then, I'd surround this code with a try catch, to catch when the conversion wouldn't work (and hence the input was not a valid input):
try:
print(int(item))
except:
print("Not a valid input")
However, given a large input size, I'm worried that this isn't the most performant way to do this, since the except
portion of the try-except really hurts performance when it runs. Is there a faster way to do this?