天天看點

python3.8報錯NameError: name ‘long‘ is not defined問題描述:原因分析:解決方案:

問題描述:

想把一長串數字字元串轉成整型,很怕int()不夠用是以才用long()轉換。結果…

報錯NameError: name ‘long’ is not defined

python3.8報錯NameError: name ‘long‘ is not defined問題描述:原因分析:解決方案:

原因分析:

Python3.x 版本已删除 long() 函數。

解決方案:

結果可以用int函數處理很長的數字字元串…

#print(long('465432641321011'))
print(int('465432641321011'))
           
python3.8報錯NameError: name ‘long‘ is not defined問題描述:原因分析:解決方案:

注意,自從Python2.2起,如果整數發生溢出,Python會自動将整數資料轉換為長整數

讓我們再複習一下類似類型轉換函數

  • int(x [,base ]) 将x轉換為一個整數
  • float(x ) 将x轉換到一個浮點數
  • complex(real [,imag ]) 建立一個複數
  • str(x ) 将對象 x 轉換為字元串
  • repr(x ) 将對象 x 轉換為表達式字元串
  • eval(str ) 用來計算在字元串中的有效Python表達式,并傳回一個對象
  • tuple(s ) 将序列 s 轉換為一個元組
  • list(s ) 将序列 s 轉換為一個清單
  • chr(x ) 将一個整數轉換為一個字元
  • unichr(x ) 将一個整數轉換為Unicode字元
  • ord(x ) 将一個字元轉換為它的整數值
  • hex(x ) 将一個整數轉換為一個十六進制字元串
  • oct(x ) 将一個整數轉換為一個八進制字元串