3 Ways To Check If String Can Convert To Integer In Python

How do you know if a string will convert to an integer in Python?

There are 3 ways to check if a string will convert to an integer in Python and these methods are: use a try-catch on the int(string) operation or perform an operation on the string to remove all integers and see if anything is left – use either the regex library or the string.replace() method.

Let’s look at each approach in a little more detail and use an example or two.

1. Use try-catch Block

The simplest way to try if a string variable will convert to an integer is to wrap the operation in a try-catch block.

This would look something a little like this:

try:
    my_int = int(my_string)
except ValueError:
    my_int = do_something_else(my_string)

In the try-catch block above you enter the initial operation you would like to have happen to your code: converting the variable my_string to an integer using the int() built-in method.

The error that will be thrown should this operation not work will be a ValueError and you will get something like this when trying to convert a string variable that cannot be converted to an integer :

>>> int("test")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'test'

Therefore, as you want to capture this error on an exception block you enter the type of error you want to handle and then instruct Python on what you want to do next. This is why the next block in the previous code had except ValueError .

Within the exception block you can then perform whatever you wish on the string variable knowing that it cannot cleanly be converted to an integer.

2. Use re.sub() Function

If you can import a library into your Python code try the Regex library and its corresponding substitute function: .sub(regex_pattern, substitute_with_string, string_to_change) .

The substitute function takes three parameters, the first being the Regex pattern to match all the digits in your original string. This can be captured easily with the digit regex flag: \d+ .

The second parameter of the substitute function is the string to replace with. In this use case, I will use an empty string '' .

The third parameter is the string or variable containing the string to perform the operation on.

Here’s how this works using the re.sub() method:

>>> import re
>>> my_string = "123"
>>> my_int = int(my_string) if len(my_string) > 0 and re.sub(r"\d+", "", my_string) == '' else None
>>> my_int
123

The reason for the initial condition in the if statement to check for the len() , length, of the string being operated on is that an empty string could be passed through and produce an error. Demonstrated here:

>>> x = ''
>>> int(x) if re.sub(r'\d+', '', x) == '' else None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

As you can see a ValueError is produced, which is not what is needed. Therefore, a condition on the length of the string being operated on is needed:

>>> x = ''
>>> int(x) if len(x) > 0 and re.sub(r'\d+', '', x) == '' else 'ha!'
'ha!'

Another alternative instead of checking against an empty string is to wrap the re.sub() method in the built-in len() function and if the result is 0 then this would mean that each character in the original source string can be replaced with an empty string leaving the original string with an empty string.

An empty string has a length of 0 .

Here’s how the code would change if using the len() function instead:

>>> import re
>>> a_string = "123"
>>> my_int = int(a_string) if len(my_string) > 0 and len(re.sub(r'\d+', '', a_string)) == 0 else None
>>> my_int
123

3. Use .replace() String Method

The corresponding approach without importing the Regular Expression library into your code is to use the built-in .replace(find_string, replace_with) string method, but this would require chaining each number individually and would look something like so:

>>> my_string = "123"
>>> my_int = int(my_string) if len(my_string) > 0 and my_string.replace('1', '').replace('2', '').replace('3', '') == "" else None
>>> my_int
123

As you can see I shortened my code by only replacing the numbers I knew were in the original string, this code would be a lot longer if you had to include all the numerical digits from 0 to 9 . Hence, why importing the regular expression and using the re library would be a cleaner approach.

Summary

To check if a string will cleanly convert to an integer in Python look at wrapping your conversion in a try-catch block, or try replacing all integer characters with an empty string and seeing if there’s only an empty string left.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.