Hit enter after type your search item

5 examples to Convert a Python string to int and float

How to convert Python string to an int and float

In certain scenarios, you may need to convert a string to an integer or float for performing certain operations in Python.

(Both of these examples are explained below along with list comprehensions and using base 16, 36, etc. values)

If you try doing some action with a “string number” like addition, subtraction, it will generate an error as shown below.

The code that will produce an error:

str_a = ’50’

b = 10

c = str_a + b

print (c)

The output:

Python string to int

You may use the Python int and float functions for converting a string to an integer and float numbers before doing any operation, as shown in the examples below.

Using int() function for converting a string into integer

Using the same code as in above example, except converting the string value into an integer by using int. See the code and output:

The code:



The output is the value of c, which is the sum of the variables str_a and b.

Converting a decimal string into float number

You may use the float class for converting a decimal string to float number. See the following example for demonstration:

The code of using float for converting a string:


Python string to float

Note: if you try using the int here, it will generate an error.

Converting string numbers in a Python list to integers

If you intend to convert string numbers contained in a python list, then one of the ways to convert those strings into the int is using the list comprehension.

A new list will be created where you may use int in each iteration as shown in the example below:

The code:


list string to int

The output is a new list with strings converted into integers.

Converting decimal list into float for list items

Similarly, you may use the float instead of int for converting a list containing decimal number strings. See a demonstration below:

The code for converting list items to float:


list string to float

An example of converting a string to int with commas

Well, what about string numbers with commas like “5,000,000”. If you try to convert this by using int or float, it will generate an error.

The solution to this can be importing locale:

import locale

and using the local setting like for USA:

locale.setlocale( locale.LC_ALL, ‘en_US.UTF-8’ )

However, this may cause problems with different locations. One other solution is to replace the comma with nothing i.e. ” and then use the int, as shown in the example below:

The code:


string to int comma

In the output, you can see a flat integer value. The same can be applied as converting a string to float.

The example of using base argument for string to int

As such, you may also use the Python int function as follows:

class int(x, base=10)

That means you may provide base value for conversion as well. In the example below, I used various base values in the int function for the string to int conversion:

The output:


An example of base 16

The example below uses base 16 value in the int() function:

The output:


Similarly, you may convert to binary and octal as well. For learning more about this, go to int() function tutorial.

An example of string to integer for leap years

The input entered by the user in Python program is taken as a string. In the following example, a user is asked to enter a year. After entering the value, the program tells if the entered year is a leap year or not.

For that, the entered value is first converted to int and then leap year formula is used to establish if it is a leap year or not.

The output:

string to integer

You see, the entered string is converted upfront to integer and then used for calculation.

Note: Leap year has two other conditions:

  • (i) Year is not divisible by 100. For example, 1900 is not a leap year, although it can be divided by 4.
  • (ii)Year can be divided by 400 is a leap year. For example, 2000 is a leap year as it can be divided by 400 (though it can be divided by 100 as well).
  • (iii)You need to incorporate these two conditions as well in the above code by using if..else if ladder etc.

How to convert int to string?

In case you require converting the integers to string, you may use the str() function of Python. The str() function takes an object that can be an int, float, double etc.

See this simple example of converting an int type variable to a string and then this variable is used with a string variable for concatenation:

The output:

str function for int to string456

If int variable was used without the str() function in the above example, a TypeError error should have been produced.

You may learn more about the str() function for converting int to strings.

This div height required for enabling the sticky sidebar