If you want to convert a string of numbers to numeric values in Python, use int() to convert to integers and float() to convert to floating point numbers.
The following is explained here, along with sample code.
- Basic usage
- Convert numeric strings to integers:
int()
- Convert a string of numbers to floating point numbers:
float()
- Convert numeric strings to integers:
- Special Usage
- Converts strings in binary, octal, and hexadecimal notation to numbers
- Converts strings in exponential notation to numerical values
- Convert full-width Arabic numeral strings to numbers
- Convert a string of Chinese characters to numbers
To convert a numeric value to a string, use str().
If you want to convert numbers or strings to various formats, use the format() function or the string method str.format(). Then you can convert to 0-fill, binary, octal, hexadecimal, exponential notation, etc. See the following article for details.
It can also convert a list of strings into a list of numbers. See the following article for details.
- Convert numeric strings to integers: int()
- Convert a string of numbers to floating point numbers: float()
- Converts strings in binary, octal, and hexadecimal notation to numbers
- Converts strings in exponential notation to numerical values
- Convert full-width Arabic numeral strings to numbers
- Convert a string of Chinese characters to numbers
Convert numeric strings to integers: int()
You can use int() to convert a string of numbers to integer type numbers.
print(int('100'))
print(type(int('100')))
# 100
# <class 'int'>
Decimals, including decimal points, and strings separated by commas will result in a ValueError.
# 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'
Comma-delimited strings can be converted by removing the comma (replacing it with an empty string) using the replace() method.
print(int('10,000'.replace(',', '')))
# 10000
Convert a string of numbers to floating point numbers: float()
A float() can be used to convert a string of numbers to a floating-point number type.
print(float('1.23'))
print(type(float('1.23')))
# 1.23
# <class 'float'>
Strings with the integer part omitted are converted by complementing the integer part with 0.
print(float('.23'))
# 0.23
Integer strings are also converted to floating-point numbers.
print(float('100'))
print(type(float('100')))
# 100.0
# <class 'float'>
Converts strings in binary, octal, and hexadecimal notation to numbers
If a radix is specified as the second argument to int(), the string can be converted to an integer int by considering it as a binary, octal, hexadecimal, etc.
print(int('100', 2))
print(int('100', 8))
print(int('100', 16))
# 4
# 64
# 256
As in the previous examples, if omitted, the number is considered to be a decimal number.
print(int('100', 10))
print(int('100'))
# 100
# 100
If the radix is set to 0, the conversion is based on the string prefix. See below for string prefixes.
0b
0B
0o
0O
0x
0X
print(int('0b100', 0))
print(int('0o100', 0))
print(int('0x100', 0))
# 4
# 64
# 256
Prefixes and hex alphabets may be either uppercase or lowercase.
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
See the following article for information on the interconversion of binary, octal, and hexadecimal numbers and strings.
Converts strings in exponential notation to numerical values
Strings in exponential notation can be converted directly to float type with 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'>
Lowercase e may also be capitalized E.
print(float('1.23E-4'))
# 0.000123
Convert full-width Arabic numeral strings to numbers
Full-width Arabic numerals can be converted directly to numbers by int() or float().
print(int('100'))
print(type(int('100')))
# 100
# <class 'int'>
print(float('100'))
print(type(float('100')))
# 100.0
# <class 'float'>
However, if symbols such as minus and decimal periods are full-width characters, a ValueError will be generated.
# print(float('ー1.23'))
# ValueError: could not convert string to float: '1.23'
Numbers can be converted without problems if they are full-width characters, but minus and decimal points are half-width characters. conversion is possible by replacing full-width symbols with half-width symbols using the replace() method.
print(float('-1.23'))
# -1.23
print(float('ー1.23'.replace('ー', '-').replace('.', '.')))
# -1.23
Convert a string of Chinese characters to numbers
The unicodedata.numeric() function in the unicodedata module can be used to convert a single Unicode Chinese character into a floating-point number type number.
If it is not a single letter, an error will occur. Also, non-numeric characters will cause an error.
import unicodedata
print(unicodedata.numeric('五'))
print(type(unicodedata.numeric('五')))
# 5.0
# <class 'float'>
print(unicodedata.numeric('十'))
# 10.0
print(unicodedata.numeric('参'))
# 3.0
print(unicodedata.numeric('億'))
# 100000000.0
# print(unicodedata.numeric('五十'))
# TypeError: numeric() argument 1 must be a unicode character, not str
# print(unicodedata.numeric('漢'))
# ValueError: not a numeric character