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.

An example of string to int conversion
A demo of string to float conversion

(Both of these examples are explained below along with list comprehensions)

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

See online demo and code

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:

int

See online demo and code

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:

Python string to float

See online demo and code

The code of using float for converting a string:

 

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:

list string to int

See online demo and code

The code:

 

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:

list string to float

See online demo and code

The code for converting list items 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 comma by nothing i.e. ” and then use the int, as shown in the example below:

string to int comma

See online demo and code

The code:

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

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.

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 above example, a TypeError error should have produced.

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