Convert a NoneType to an Integer or String in Python

There are many data types in Python. One of them is NoneType.

It is a special Python object that has no value. It is used to indicate the absence of a value, or to signify that no data is available.

The following are ways in which we can convert a NoneType to an integer or string.

Method 1: Using the Boolean OR operator

First of all, let’s see some examples:

Output:

99

Output:

Fort-Ternan

In the above example, the variables var1 and var2 hold NoneTypes. Using OR operator, we can convert these NoneTypes to an integer or a string of our choosing.

But why does this work? There are two points to consider to answer this question.

Point 1: Understanding how OR boolean operator works

The general syntax for the OR operator is

expression1 or expression2

where

expression1 expression2 expression1 or expression2
True True True
True False True
False True True
False False False
Table 1: How to use the OR operator works.

Point 2: Python considers some values as false

These values include: 0, None, False, [ ] (empty list), ( ) (empty type), { } (empty dictionary), set() (empty set), and range(0) (empty range).

When the rules in the Table 1 above are used with Point 2, it is clear that “None or 99” evaluates to 99 because None is understood as a false value and 99 as true. Here are more examples.

Output:

val1:  7.89
val2:  [ ]
val3:  456
val4:  2.5

Note: “x or y” evaluates to x if x is true; otherwise, y is returned. In other words, if x is true, it is returned irrespective of the value of y, but if x is false, y is returned irrespective of its value.

A more interesting example

Suppose we want to convert all the elements in the following list into integers: [2.36, -1.87, None, 9.6]. The first way could be to loop through the list converting each item as follows.

Output:

[2, -1, 99, 9]

Alternatively, we can use list comprehension as shown below.

Output:

[2, -1, 99, 9]

As shown in the output, the None value is converted to 99 because None or 99 evaluates to 99.

Method 2: Using the if-condition

In this method, we use the if-statement to check if the variable holds a None value and if that is the case we reassign an integer or a string as shown in the coming examples.

Output:

99

If var3 is None (which it is) we reassign the variable var3 to a new value of 99, otherwise we set it to the original value of var3. We can write the same code as one-liners as shown in the following examples

Output:

var4:  99
var5:  12.6
var6:  Missing
var7:  3

Here is how we can use the if-statement to convert each of the elements of the following list into an integer: [2.36, -1.87, None, 9.6] using one-liners.

Output:

converted_list1:  [2, -1, 99, 9]

In the code, we loop through list1, and at each iteration check if the element is None if it is set it to 99 and if not cast it into an integer using the int() function.

Conclusion

A NoneType can be converted into an integer or a string in two ways that we discussed in this article – using the boolean OR operator or by if-statement. In either of the cases, you can use the int() function to cast the result into an integer if needed.