Python入門シリーズ——8.タイプ変換

1099 ワード

変数タイプの指定
何度も変数にタイプを強制したいと思っていますが、 を使用することができます.Pythonはオブジェクト向け言語なので、クラスでベースタイプを定義することができます.
Pythonで変換を実現するには,以下のコンストラクション関数を用いることができる.
  • int()

  • int,float(四捨五入をサポート)、string字面量の値をintタイプに変換することを表す.
    
    x = int(1)   # x will be 1
    y = int(2.8) # y will be 2
    z = int("3") # z will be 3
    
  • float()

  • int,float(四捨五入をサポート)、string字面量の値をfloatタイプに変換することを表す.
    
    x = float(1)     # x will be 1.0
    y = float(2.8)   # y will be 2.8
    z = float("3")   # z will be 3.0
    w = float("4.2") # w will be 4.2
    
  • str()

  • int,float(四捨五入をサポート)、string字面量の値をstringタイプに変換することを表す.
    
    x = str("s1") # x will be 's1'
    y = str(2)    # y will be '2'
    z = str(3.0)  # z will be '3.0'
    

    翻訳リンク:
    https://www.w3schools.com/pyt...
    もっと高品質の乾物:私のGitHub:pythonを参照