In Python, you can convert a string str
to an integer int
and a floating point number float
with int[]
and float[]
.
This article describes the following contents.
- Convert a string to
int
:int[]
- Convert a string to
float
:float[]
- Convert a string of binary, octal, and hexadecimal notation to
int
- Convert a string of exponential notation to
float
Use str[]
to convert an integer or
floating point number to a string.
- Built-in Functions - str[] — Python 3.9.0 documentation
You can also convert a list of strings to a list of numbers. See the following article.
- Convert a list of strings and a list of numbers to each other in Python
Convert a string to int
: int[]
int[]
converts a string to an integer int
.
- Built-in Functions - int[] — Python 3.9.0 documentation
print[int['100']]
print[type[int['100']]]
# 100
#
A string containing .
or ,
raises an error.
# 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'
A comma-separated string can be converted by deleting ,
with replace[]
[replace with the empty string
''
].
print[int['10,000'.replace[',', '']]]
# 10000
See the following article for replace[]
.
- Replace strings in Python [replace, translate, re.sub, re.subn]
Convert a string to float
: float[]
float[]
converts a string to a floating point number float
.
- Built-in Functions - float[] — Python 3.9.0 documentation
print[float['1.23']]
print[type[float['1.23']]]
# 1.23
#
A string without the integer part can also be converted.
print[float['.23']]
# 0.23
A string representing an integer is also converted to a floating point number float
.
print[float['100']]
print[type[float['100']]]
# 100.0
#
Convert a string of binary, octal, and hexadecimal notation to
int
If you pass a base number to the second argument of int[]
, a string is treated as binary, octal, and hexadecimal.
print[int['100', 2]]
print[int['100', 8]]
print[int['100', 16]]
# 4
# 64
# 256
If omitted, it is regarded as decimal, like in previous examples.
print[int['100', 10]]
print[int['100']]
# 100
# 100
If the base is 0
, it is converted based on the prefix of the string [0b
, 0o
, 0x
or 0B
, 0O
, 0X
].
print[int['0b100', 0]]
print[int['0o100', 0]]
print[int['0x100', 0]]
# 4
# 64
# 256
Prefixes and hexadecimal alphabets may be written in either upper or lower case.
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 more information on the interconversion of binary, octal, and hexadecimal string and int
, see the following article.
- Convert binary, octal, decimal, and hexadecimal in Python
Convert a string of exponential notation to float
A string of exponential notation can be converted to float
with
float[]
.
print[float['1.23e-4']]
print[type[float['1.23e-4']]]
# 0.000123
#
print[float['1.23e4']]
print[type[float['1.23e4']]]
# 12300.0
#
You can use both e
and E
.
print[float['1.23E-4']]
# 0.000123