How do you convert a string to an integer or a float in python?

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 542.2222? Or parse the string "31" to an integer, 31? I just want to know how to parse a float string to a float, and (separately) an int string to an int.

It's good that you ask to do these separately. If you're mixing them, you may be setting yourself up for problems later. The simple answer is:

"545.2222" to float:

>>> float("545.2222")
545.2222

"31" to an integer:

>>> int("31")
31

Other conversions, ints to and from strings and literals:

Conversions from various bases, and you should know the base in advance (10 is the default). Note you can prefix them with what Python expects for its literals (see below) or remove the prefix:

>>> int("0b11111", 2)
31
>>> int("11111", 2)
31
>>> int('0o37', 8)
31
>>> int('37', 8)
31
>>> int('0x1f', 16)
31
>>> int('1f', 16)
31

If you don't know the base in advance, but you do know they will have the correct prefix, Python can infer this for you if you pass 0 as the base:

>>> int("0b11111", 0)
31
>>> int('0o37', 0)
31
>>> int('0x1f', 0)
31

Non-Decimal (i.e. Integer) Literals from other Bases

If your motivation is to have your own code clearly represent hard-coded specific values, however, you may not need to convert from the bases - you can let Python do it for you automatically with the correct syntax.

You can use the apropos prefixes to get automatic conversion to integers with the following literals. These are valid for Python 2 and 3:

Binary, prefix 0b

>>> 0b11111
31

Octal, prefix 0o

>>> 0o37
31

Hexadecimal, prefix 0x

>>> 0x1f
31

This can be useful when describing binary flags, file permissions in code, or hex values for colors - for example, note no quotes:

>>> 0b10101 # binary flags
21
>>> 0o755 # read, write, execute perms for owner, read & ex for group & others
493
>>> 0xffffff # the color, white, max values for red, green, and blue
16777215

Making ambiguous Python 2 octals compatible with Python 3

If you see an integer that starts with a 0, in Python 2, this is (deprecated) octal syntax.

>>> 037
31

It is bad because it looks like the value should be 37. So in Python 3, it now raises a SyntaxError:

>>> 037
  File "", line 1
    037
      ^
SyntaxError: invalid token

Convert your Python 2 octals to octals that work in both 2 and 3 with the 0o prefix:

>>> 0o37
31

In this example, you will learn to parse a string to a float or int.

To understand this example, you should have the knowledge of the following Python programming topics:

  • Python Data Types
  • Python Strings

Example 1: Parse string into integer

balance_str = "1500"
balance_int = int(balance_str)

# print the type
print(type(balance_int))

# print the value
print(balance_int)

Output


1500

int() can be used to parse a string to an integer. The argument passed balance_int is the string. As shown in the above example, you can see the type of the string changed to int.

Note: The string must be a numeral value.


Example 2: Parse string into float

balance_str = "1500.4"
balance_float = float(balance_str)

# print the type
print(type(balance_float))

# print the value
print(balance_float)

Output


1500.4

float() can be used to parse a string to an integer. Similar to Example 1, the string is passed as an argument to float().


Example 3: A string float numeral into integer

balance_str = "1500.34"
balance_int = int(float(balance_str))

# print the type
print(type(balance_int))

# print the value
print(balance_int)

Output


1500

If the string is a float numeral, you can convert it into a float type using float(), and then parse it to an integer using int().

Introduction

In Python, data types are used to classify one particular type of data, determining the values that you can assign to the type and the operations you can perform on it. When programming, there are times we need to convert values between types in order to manipulate values in a different way. For example, we may need to concatenate numeric values with strings, or represent decimal places in numbers that were initialized as integer values.

This tutorial will guide you through converting numbers, strings, tuples and lists, as well as provide examples to help familiarize yourself with different use cases.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Converting Number Types

In Python, there are two number data types: integers and floating-point numbers or floats. Sometimes you are working on someone else’s code and will need to convert an integer to a float or vice versa, or you may find that you have been using an integer when what you really need is a float. Python has built-in methods to allow you to easily convert integers to floats and floats to integers.

Converting Integers to Floats

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

Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3 command. Then you can copy, paste, or edit the examples by adding them after the >>> prompt.

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))

Output

57.0

By using the float() function, we can convert integers to floats.

Converting Floats to Integers

Python also has a built-in function to convert floats to integers: int().

The int() function works similarly to the float() function: you can add a floating-point number inside of the parentheses to convert it to an integer:

int(390.8)

In this case, 390.8 will be converted to 390.

You can also use this with variables. Let’s declare b as equal to 125.0, and c as equal to 390.8, then print out the new floats:

b = 125.0
c = 390.8

print(int(b))
print(int(c))

Output

125 390

When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer. Even though we may want to round 390.8 up to 391, Python will not do this through the int() function.

Numbers Converted Through Division

In Python 3, relevant quotients are converted from integers to floats when doing division though they are not in Python 2. That is, when you divide 5 by 2, in Python 3 you will get a float for an answer (2.5):

a = 5 / 2
print(a)

Output

2.5

In Python 2, since you were dealing with two integers, you would receive an integer back as your answer, instead: 5 / 2 = 2. Read “Python 2 vs Python 3: Practical Considerations” for more information about the differences between Python 2 and Python 3.

Converting with Strings

A string is a sequence of one or more characters (letters, numbers, symbols). Strings are a common form of data in computer programs, and we may need to convert strings to numbers or numbers to strings fairly often, especially when we are taking in user-generated data.

Converting Numbers to Strings

We can convert numbers to strings through using the str() method. We’ll pass either a number or a variable into the parentheses of the method and then that numeric value will be converted into a string value.

Let’s first look at converting integers. To convert the integer 12 to a string value, you can pass 12 into the str() method:

str(12)

When running str(12) in the Python interactive shell with the python command in a terminal window, you’ll receive the following output:

Output

'12'

The quotes around the number 12 signify that the number is no longer an integer but is now a string value.

With variables we can begin to see how practical it can be to convert integers to strings. Let’s say we want to keep track of a user’s daily programming progress and are inputting how many lines of code they write at a time. We would like to show this feedback to the user and will be printing out string and integer values at the same time:

user = "Sammy"
lines = 50

print("Congratulations, " + user + "! You just wrote " + lines + " lines of code.")

When we run this code, we receive the following error:

Output

TypeError: can only concatenate str (not "int") to str

We’re not able to concatenate strings and integers in Python, so we’ll have to convert the variable lines to be a string value:

user = "Sammy"
lines = 50

print("Congratulations, " + user + "! You just wrote " + str(lines) + " lines of code.")

Now, when we run the code, we receive the following output that congratulates our user on their progress:

Output

Congratulations, Sammy! You just wrote 50 lines of code.

If we are looking to convert a float to a string rather than an integer to a string, we follow the same steps and format. When we pass a float into the str() method, a string value of the float will be returned. We can use either the float value itself or a variable:

print(str(421.034))

f = 5524.53
print(str(f))

Output

421.034 5524.53

We can test to make sure it’s right by concatenating with a string:

f = 5524.53
print("Sammy has " + str(f) + " points.")

Output

Sammy has 5524.53 points.

We can be sure our float was properly converted to a string because the concatenation was performed without error.

Converting Strings to Numbers

Strings can be converted to numbers by using the int() and float() methods.

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 the 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)

Output

TypeError: 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 values these that were originally strings.

lines_yesterday = "50"
lines_today = "108"

lines_more = int(lines_today) - int(lines_yesterday)

print(lines_more)

Output

58

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

We can also convert the numbers in the example above to float values by using the float() method in place of the int() method. Instead of receiving the output of 58, we’ll receive the output of 58.0, a float.

The user Sammy is earning points in decimal values

total_points = "5524.53"
new_points = "45.30"

new_total_points = total_points + new_points

print(new_total_points)

Output

5524.5345.30

In this case, using the + operand with two strings is a valid operation, but it is concatenating two strings rather than adding two numeric values together. So, our output looks unusual since it just places the two values next to each other.

We’ll want to convert these strings to floats prior to performing any math with the float() method:

total_points = "5524.53"
new_points = "45.30"

new_total_points = float(total_points) + float(new_points)

print(new_total_points)

Output

5569.83

Now that we have converted the two strings to floats, we receive the anticipated result that adds 45.30 to 5524.53.

If we try to convert a string value with decimal places to an integer, we’ll receive an error:

f = "54.23"
print(int(f))

Output

ValueError: invalid literal for int() with base 10: '54.23'

If we pass a decimal value in a string to the int() method we’ll receive an error because it will not convert to an integer.

Converting strings to numbers enables us to quickly modify the data type we are working with so that we can perform operations on numeric values that were originally cast as strings.

Converting to Tuples and Lists

You can use the methods list() and tuple() to convert the values passed to them into the list and tuple data type respectively. In Python:

  • a list is a mutable ordered sequence of elements that is contained within square brackets [ ].
  • a tuple is an immutable ordered sequence of elements contained within parentheses ( ).

Converting to Tuples

Let’s start with converting a list to a tuple. Converting a list to a tuple, because it’s an immutable data type, can allow substantial optimization to the programs that we create. When we use the method tuple() it will return the tuple version of the value passed to it.

print(tuple(['pull request', 'open source', 'repository', 'branch']))

Output

('pull request', 'open source', 'repository', 'branch')

We see that a tuple is printed out in the output, as the items are now contained within parentheses rather than square brackets.

Let’s use tuple() with a variable that represents a list:

sea_creatures = ['shark', 'cuttlefish', 'squid', 'mantis shrimp']
print(tuple(sea_creatures))

Output

('shark', 'cuttlefish', 'squid', 'mantis shrimp')

Again, we see that the list value is changed to a tuple value, indicated by the parentheses. We can convert any iterable type to a tuple, including strings:

print(tuple('Sammy'))

Output

('S', 'a', 'm', 'm', 'y')

Because we can iterate through strings, we can convert them to tuples with the tuple() method. With data types that are not iterable, however, like integers and floats, we will receive a type error:

print(tuple(5000))

Output

TypeError: 'int' object is not iterable

While it is possible to convert the integer to a string and then convert to a tuple, as in tuple(str(5000)), it is best to opt for readable code over complicated conversions.

Converting to Lists

Converting values, especially tuples, to lists can be useful when you need to have a mutable version of that value.

We’ll use the list() method to convert the following tuple to a list. Because the syntax for creating a list uses parentheses, be sure to include the parentheses of the list() method, and in this case the print() method as well:

print(list(('blue coral', 'staghorn coral', 'pillar coral')))

Output

['blue coral', 'staghorn coral', 'pillar coral']

The square brackets signal that a list has been returned from the original tuple value that was passed through the list() method.

To make the code more readable, we can remove one of the pairs of parentheses by using a variable:

coral = ('blue coral', 'staghorn coral', 'pillar coral')
list(coral)

If we print list(coral) we would receive the same output as above.

Just like tuples, strings can be converted to lists:

print(list('shark'))

Output

['s', 'h', 'a', 'r', 'k']

Here the string 'shark' was converted to a list, providing a mutable version of the original value.

Conclusion

This Python tutorial demonstrated how to convert several of the important native data types to other data types, primarily through built-in methods. Being able to convert data types in Python provides you with extra flexibility when writing your programs.

How do I convert a string to a float in Python?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

Can you convert string to int or float?

Strings can be converted to numbers by using the int() and float() methods.

How do you convert string to integer in Python?

To convert, or cast, a string to an integer in Python, you use the int() built-in function. The function takes in as a parameter the initial string you want to convert, and returns the integer equivalent of the value you passed. The general syntax looks something like this: int("str") .

How do you change a datatype to a float in Python?

To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.