One place for hosting & domains

# Integers

## How To Convert Integers to Floats in Python 3

Python’s `float()` method will convert integers to floats. To use this function, add an integer inside of the parentheses:

``````float(57)
``````

In this case, `57` will be converted to `57.0`.

You can also use this with a variable. Let’s declare `f` as equal to `57`, and then print out the new float:

``````f = 57
print(float(f))
``````
``````Output57.0
``````

By using the `float()` function, we’ve converted an integer to a float.

If you’d like to learn more about converting different data types in Python, check out our How To Convert Data Types in Python 3 tutorial. Read more about Python in our How To Code in Python 3 series.

## How To Convert Floats to Integers in Python 3

Python’s `float()` method will convert integers to floats. To use this function, add an integer inside of the parentheses:

``````float(57)
``````

In this case, `57` will be converted to `57.0`.

You can also use this with a variable. Let’s declare `f` as equal to `57`, and then print out the new float:

``````f = 57
print(float(f))
``````
``````Output57.0
``````

By using the `float()` function, we’ve converted an integer to a float.

If you’d like to learn more about converting different data types in Python, check out our How To Convert Data Types in Python 3 tutorial. Read more about Python in our How To Code in Python 3 series.

## How To Convert Strings to Integers in Python 3

Strings can be converted to integers by using the `int()` method.

If your string does not have decimal places, you’ll most likely want to convert it to an integer by using the `int()` method.

Let’s use an example of the user sammy keeping track of lines of code written each day. We may want to manipulate those values with math to provide more interesting feedback for the user, but those values are currently stored in strings:

``````lines_yesterday = "50"
lines_today = "108"

lines_more = lines_today - lines_yesterday

print(lines_more)
``````
``````OutputTypeError: unsupported operand type(s) for -: 'str' and 'str'
``````

Because the two numeric values were stored in strings, we received an error. The operand `-` for subtraction is not a valid operand for two string values.

Let’s modify the code to include the `int()` method that will convert the strings to integers, and allow us to do math with these values that were originally strings.

``````lines_yesterday = "50"
lines_today = "108"

lines_more = int(lines_today) - int(lines_yesterday)

print(lines_more)
``````
``````Output58
``````

The variable `lines_more` is automatically an integer, and it is equal to the numeric value of `58` in this example.

If you want to learn more about converting Python data types, check out How To Convert Data Types in Python 3 as well as our How To Code in Python 3 series.