Convert Percentage String To Decimal Like “30%” To 0.3 In Python

How do you convert a string representing a percentage number into an actual number that can be used to perform calculations?

To convert a string represented as a percentage number into an actual decimal number in Python use the built-in float() function after removing the percent symbol from the string and any other characters that prevent numeric conversion.

Without purging from the original string the percent symbol you will get a ValueError as demonstrated below in the Python REPL:

>>> float("30.0%")
Traceback (most recent call last):
File "<console>", line 1, in <module>
ValueError: could not convert string to f
loat: '30.0%'

So to get the conversion to work well you need to remove characters from the original string this would include the percent symbol.

Here’s an example demonstrating the use of the float function and how you can use the built-in replace() string method to strip the percentage symbol away:

>>> float("30.0%".replace("%", ""))
30.0

As you can see from the above example you can extract the number from the original percentage string.

Now that you have a number to convert the percentage number into its decimal representation simply divide by 100.

Here’s how you could write the above in one neat little line of code:

>>> float("30.0%".replace("%", ""))/100
0.3

This is the easiest way to convert a string number with a percentage symbol.

However, there may be times other characters need to be removed to help with the conversion.

Convert Percent String Number With Comma

Another character that may prevent proper conversion of the percent string number is if the string contains a comma.

Here’s an example demonstrating the previous conversion, but using it on a string percentage number with a comma, once again you will get the all-too-familiar ValueError :

>>> float("3,000%".replace("%", ""))
Traceback (most recent call last):
File "<console>", line 1, in <module>
ValueError: could not convert string to float: '3,000.0'

Therefore, besides removing the actual % symbol, should your percentage string numbers contain commas you may need to remove that too.

Thankfully, it’s not too difficult to remove additional characters from your original string but if you still want to use the replace string method you will need to chain them together like so:

>>> float("3,000%".replace("%", "").replace(",", ""))/100
30.0

As you can see from the above example the Python code output the intended result of 30 from a percentage string number containing a comma.

Remove All Non-Numeric Characters

An easier way to manage the removal of all characters that may prove problematic with the string conversion is to just remove everything except for the numbers and the decimal place characters.

The best way to handle this type of operation is to import the Regular Expression (Regex for short) library and to use the substitute function.

The substitute function takes three parameters with the first being the regex pattern to match, the second parameter what string to substitute and third the string to operate on.

Here’s one way you could strip all the numbers out using the Regex library:

>>> import re
>>> float(re.sub(r"[^0-9.]", "", "3,000.0%"))/100
30.0

From the above code you can see the Regex library needs to be imported first and then you perform the substitution of all non-numeric and decimal place characters from the percent string.

The Regex pattern that handles this is seen in the first parameter of the sub() function r"[^0-9.]" which means that the substitution will happen on all characters not between 0 to 9 and the decimal dot character. Therefore every other character is substituted with a blank character (meaning it’s removed).

Summary

To convert a percentage string to a decimal number use the built in function float with the replace string methods like so: float("30.0%".replace("%", ""))/100 .

If the percentage string contains other characters preventing conversion you may want to import the Regex library and use the sub method like so: float(re.sub(r"[^0-9.]", "", "3,000%"))/100 .

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.