Convert a string into integer or float using Python

In this short article, we will see how to convert a string value into an integer in python.

To convert any string value into an integer we can use the in-built int() function in python. It can convert any specific value into an integer number.



value: the value that will be converted to an integer.

base: it represents the number format

Now, let’s see an example, of how the int() function is used to convert any value into an integer number.

Convert to interger number using int() function


num = '124'

print(type(num)) // <class 'str'>

Here, we have just checked the data type of the variable num using type() and as you can see it’s a string value.

The type() function returns the data-type of the objects/data in python.

Now, to convert it into an integer number we will use the int() function.

num = '123'

converted_num = int(num)

print(type(converted_num) // <class 'int'>

Now, when we pass the string value (ie. num variable) in the int() function, it converts and returns an integer value.

Convert a decimal string to int using float() in Python

Now, if the number is a decimal number (floating point) then we have to use the float() function.


num = '10.5'

converted_num = float(num)

print(type(converted_num)) // <class 'float'>

So to convert a decimal number string in python we have to use the in-built float() function.

Related Articles:

Split String and get the first element using python

Split string by whitespace in python

Replace character in String by index in Python

Python – Check if a string is Empty or Not

Python – Remove Newline From Strings (3 Ways)

How to Append String in Python

Scroll to Top