Convert a string to a number (int, float) in Python

Modified: | Tags: Python, String

In Python, to convert a string (str) to a number, use int() for integers and float() for floating point numbers.

Use str() to convert a number to a string.

To format numbers or strings into various styles like zero-padding, binary, octal, hexadecimal, or scientific notation, use the format() function or the format() method on string objects.

You can also convert a list of strings to a list of numbers.

Convert strings to int: int()

int() converts a string to an integer (int).

print(int('100'))
print(type(int('100')))
# 100
# <class 'int'>

Strings with . or , will raise an error in int().

# print(int('1.23'))
# ValueError: invalid literal for int() with base 10: '1.23'

# print(int('10,000'))
# ValueError: invalid literal for int() with base 10: '10,000'

To convert a comma-separated string, replace commas with an empty string ('') using replace().

print(int('10,000'.replace(',', '')))
# 10000

For more about replace(), refer to the following article.

In versions released after September 7, 2022 (Python 3.11, 3.10.7, 3.9.14, 3.8.14, 3.7.14, and later), integer-string conversion is limited to 4300 digits by default. More details later.

Convert strings to float: float()

float() converts a string to a floating point number (float).

print(float('1.23'))
print(type(float('1.23')))
# 1.23
# <class 'float'>

Strings with only a fractional part and those representing integers can be converted to floating point numbers using float().

print(float('.23'))
# 0.23

print(float('100'))
print(type(float('100')))
# 100.0
# <class 'float'>

Convert binary, octal, and hexadecimal strings to int

By specifying a base as the second argument in int(), strings in binary, octal, or hexadecimal notation can be converted to integers.

print(int('100', 2))
print(int('100', 8))
print(int('100', 16))
# 4
# 64
# 256

The base defaults to 10, treating the string as a decimal.

print(int('100', 10))
print(int('100'))
# 100
# 100

Setting the base to 0 allows the string's prefix (0b, 0o, 0x, 0B, 0O, 0X) to determine the conversion.

print(int('0b100', 0))
print(int('0o100', 0))
print(int('0x100', 0))
# 4
# 64
# 256

Both uppercase and lowercase are acceptable for prefixes and hexadecimal letters.

print(int('FF', 16))
print(int('ff', 16))
# 255
# 255

print(int('0xFF', 0))
print(int('0XFF', 0))
print(int('0xff', 0))
print(int('0Xff', 0))
# 255
# 255
# 255
# 255

For binary, octal, and hexadecimal conversion details, see the following article.

Convert scientific notation strings to float

Strings in scientific notation can be converted to floating point numbers using float().

print(float('1.23e-4'))
print(type(float('1.23e-4')))
# 0.000123
# <class 'float'>

print(float('1.23e4'))
print(type(float('1.23e4')))
# 12300.0
# <class 'float'>

Both lowercase e and uppercase E are acceptable.

print(float('1.23E-4'))
# 0.000123

Integer string conversion length limitation

In versions released after September 7, 2022 (Python 3.11, 3.10.7, 3.9.14, 3.8.14, 3.7.14, and later), integer-string conversion is limited to 4300 digits by default.

This limitation aims to prevent DoS attacks that exploit the O(n^2) complexity of integer-string conversions.

Exceeding 4300 digits in int() and str() results in ValueError.

i = int('1' * 5)
print(i)
# 11111

i = int('1' * 4300)

# i = int('1' * 4301)
# ValueError: Exceeds the limit (4300 digits) for integer string conversion: value has 4301 digits; use sys.set_int_max_str_digits() to increase the limit

s = str(10**5)
print(s)
# 100000

s = str(10**4299)

# s = str(10**4300)
# ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit

Note that this also affects internal string conversions in functions like print() and repr().

i = 10**10000
# print(i)
# ValueError: Exceeds the limit (4300 digits) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit

As introduced in the next section, conversions with bases that are powers of 2 (binary, octal, hexadecimal, etc.) are unlimited. For affected APIs, refer to the official documentation.

For cases with bases as powers of 2

Conversions where the base is a power of 2, such as binary, octal, or hexadecimal, are not limited.

For example, conversions using int(string, base) with bases such as 2, 4, 8, 16, 32, as well as conversions from integers to strings using bin(), oct(), or hex(), do not result in errors, even for values exceeding 4300 digits.

i = int('1' * 10000, base=16)

s = hex(10**10000)

Set the limit

The digit limit can be set using sys.set_int_max_str_digits(). Setting it to 0 removes the limit.

import sys

sys.set_int_max_str_digits(1000)

# i = int('1' * 1001)
# ValueError: Exceeds the limit (1000 digits) for integer string conversion: value has 1001 digits; use sys.set_int_max_str_digits() to increase the limit

sys.set_int_max_str_digits(0)

i = int('1' * 100000)

Additionally, the limit can be configured using the environment variable, PYTHONINTMAXSTRDIGITS, or the command-line option, -X int_max_str_digits. For more details, see the official documentation.

As stated, this limit is for security reasons, and should be modified with caution, especially in applications handling external input.

Related Categories

Related Articles