To convert a string to an integer you use the handy
int()
function. For example, if you have a string such as
"79"
and you insert this like so into the
int()
function:
int("79")
you get the result
79
. But what happens when you don’t have a string that easily converts into a number?
Here are some examples where converting a string into an integer type simply will not work in Python (for obvious reasons):
>>> int("Hello world")
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello world'
>>> int("12.3")
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12.3'
>>> int("12%")
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12%'
>>> int("1+1")
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1+1'
>>> int("")
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
As you can see from the above results all produce errors as the string inserted cannot be easily parsed into an integer.
It then comes back to how you want to define the resulting number if it does error.
Therefore, you could wrap each
int()
call in a
try-catch
block to see if it does error, and if it does to return a default number you would like to return. Here’s an example:
list_strings = ["1", "2.2", "3%", ""]
result = []
for i in list_strings:
try:
e = int(i)
result.append(e)
except:
result.append(0)
print(result)
> [1, 0, 0, 0]
In our simple example above we have a list of strings that we would like to convert to integers using the
int()
Python function
. We loop through each element in the list using a simple for-loop, but within the loop you can see there is a
try-catch
block.
The
try
section tests the conversion of the element in the list to an integer, and if it succeeds it inserts the result into our
result
list.
If it fails it then moves to the
except
section where, as shown in the example above, we make all errors a value of 0 which is appended to our resulting list.
However, we can do further checks on why the string failed to convert to an integer in the
exception
section. For example, we could have written the following to perform more checks in this
except
block and to give different answers according to each:
list_strings = ["1", "2.2", "3%", ""]
result = []
for i in list_strings:
try:
e = int(i)
result.append(e)
except:
if i == "":
result.append(0)
else:
result.append(None)
print(result)
> [1, None, None, 0]
Here I explicitly check if there is an empty string in the list, and if there is to convert this to the integer
0
, everything else I convert to
None
.
Without Using Try-Catch Block
If you want to
convert strings to integers in Python
without using a try-catch block then you will need to place more checks on each element
prior to
calling the
int()
function.
One way of performing a check is to use regular expressions, as shown below:
import re
list_strings = ["1", "2.2", "3%", ""]
result = []
for i in list_strings:
r = re.findall(r'\D', i)
if len(r) == 0 and len(i) > 0:
e = int(i)
result.append(e)
else:
if i == "":
result.append(0)
else:
result.append(None)
print(result)
> [1, None, None, 0]
In the above block notice the following, first, you need to import the regular expression library in Python, this is done at the top of the script using
import re
.
The next change is the regular expression capture with the statement:
r = re.findall(r'\D', i)
The function
findall()
returns a list of results according to the regular expression condition inserted into the first parameter performed on the string in the second parameter.
The regular expression
\D
means to capture all non-
d
igits.
Therefore, if the variable
r
contains an empty list then we know that there are no non-digits in the element, and this is why on the next line we check to see if the length of the variable
r
is
0
. However, we also need to check if the length of the element is greater than 0 as our empty string
will satisfy
the regular expression requirement, but we know it will error when sent through for integer conversion.
Lastly, we swap out our
except
clause for
else
and do as we have done with those elements in the list that will not play nicely with the
int()
function.
As you can see our result is exactly the same as before with the
try-catch
block.
Summary
Converting an empty string to an integer cannot be done in Python using the
int()
function and would need to be filtered away from being inserted into the
int()
function. Two ways of applying this filtering are to use a
try-catch
block, or some other form of filtering such as a regular expression. Once the empty string is captured you can manually set the value according to your use case.