How to convert a string to an integer in Python
In this tutorial, we are going to learn how to convert the string to an integer in Python.
Consider, we have a following integer in our code:
num = "21"
Now, we need to convert the above string "21"
to a integer 21
.
Converting string to integer
To convert a string to an integer, we can use the built-in int()
function in Python.
Here is an example, that converts the string 21
to an integer:
a = '21'
print(int(a)) # prints number 21
Output:
21