How to Convert String into Integer in Python?

Last updated on December 28, 2022 by Ayaz Ali Shah

You can use Python’s built-in int() function to convert the string into an integer. It accepts two parameters which first is required and the second is optional. To begin with, let’s create a pens variable and assign a string-based numeric value.

pens = '15'

Before converting it into an integer, check the pens’ data type. To check the data type of the variable, we can use the built-in function type().

# Output: <class 'str'>

Here, the type() function returns the string data type of the pens variable. Next, we need to convert the pens’ data type into an integer, to do that pass the pens variable as an argument into the int() function.

The int() is a built-in function that converts a given value’s data type into an integer.

Next, pass the pens variable into the int() function for converting its type into an integer and assign it to the new variable convertIntoInteger.

convertIntoInteger = int(pens)
# Output: 15

Above, the convertIntoInteger returns the same value that was assigned to the pens variable, but its type should be an integer. Let’s check its type through the type function.

# Output: <class 'int'>


This article demonstrates converting the string data type of a variable into an integer in python. To do that, we used the python built-in int() function.

Share on:

Related Posts

Tags: Integer, Python, String,