將數字字符串 str 轉換為數字 int,在 Python 中浮動

商業

如果要在 Python 中將一串數字轉換為數值,請使用 int() 轉換為整數,使用 float() 轉換為浮點數。

此處解釋了以下內容以及示例代碼。

  • 基本用法
    • 將數字字符串轉換為整數:int()
    • 將一串數字轉換為浮點數:float()
  • 特殊用途
    • 將二進制、八進制和十六進製表示法的字符串轉換為數字
    • 將指數符號中的字符串轉換為數值
    • 將全角阿拉伯數字字符串轉換為數字
    • 將一串漢字轉換為數字

要將數值轉換為字符串,請使用 str()。

如果要將數字或字符串轉換為各種格式,請使用 format() 函數或字符串方法 str.format()。然後就可以轉換成0-fill、二進制、八進制、十六進制、指數表示法等。詳情請看下面的文章。

它還可以將字符串列表轉換為數字列表。有關詳細信息,請參閱以下文章。

將數字字符串轉換為整數:int()

您可以使用 int() 將一串數字轉換為整數類型的數字。

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

小數(包括小數點)和用逗號分隔的字符串將導致 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'

可以通過使用 replace() 方法刪除逗號(用空字符串替換它)來轉換逗號分隔的字符串。

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

將一串數字轉換為浮點數:float()

float() 可用於將一串數字轉換為浮點數類型。

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

省略整數部分的字符串通過用 0 對整數部分進行補碼進行轉換。

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

整數字符串也被轉換為浮點數。

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

將二進制、八進制和十六進製表示法的字符串轉換為數字

如果將基數指定為 int() 的第二個參數,則可以將字符串轉換為整數 int,方法是將其視為二進制、八進制、十六進制等。

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

與前面的示例一樣,如果省略,則該數字被認為是十進制數。

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

如果基數設置為 0,則轉換基於字符串前綴。有關字符串前綴,請參見下文。

  • 0b
    • 0B
  • 0o
    • 0O
  • 0x
    • 0X
print(int('0b100', 0))
print(int('0o100', 0))
print(int('0x100', 0))
# 4
# 64
# 256

前綴和十六進製字母可以是大寫或小寫。

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

有關二進制、八進制和十六進制數字和字符串的相互轉換的信息,請參閱以下文章。

將指數符號中的字符串轉換為數值

指數符號中的字符串可以使用 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'>

小寫的 e 也可以大寫 E。

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

將全角阿拉伯數字字符串轉換為數字

全角阿拉伯數字可以通過 int() 或 float() 直接轉換為數字。

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

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

但是,如果減號和小數點等符號是全角字符,則會產生 ValueError。

# print(float('ー1.23'))
# ValueError: could not convert string to float: '1.23'

如果數字是全角字符,則可以毫無問題地轉換數字,但減號和小數點是半角字符。通過使用 replace() 方法將全角符號替換為半角符號,可以進行轉換。

print(float('-1.23'))
# -1.23

print(float('ー1.23'.replace('ー', '-').replace('.', '.')))
# -1.23

將一串漢字轉換為數字

unicodedata 模塊中的 unicodedata.numeric() 函數可用於將單個 Unicode 漢字轉換為浮點數類型的數字。

如果不是單個字母,則會發生錯誤。此外,非數字字符會導致錯誤。

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