Pythonで数値のみで構成された文字列を数値に変換するintと数値を文字列に変換するstrについて紹介します。
htmlinsert(): The given local file does not exist or is not readable.
$ python --version Python 2.7.3
$ lsb_release -d Description: Ubuntu 12.04.4 LTS
以下の通り、変数a,b,cを使ってintとstrを使って変換しています。
typeによりクラスを表示し確認しています。
$ python Python 2.7.3 (default, Sep 26 2013, 20:03:06) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> i = 100 >>> s = "200" >>> type(i) <type 'int'> >>> type(s) <type 'str'> >>> c = int(s) >>> c 200 >>> type(c) <type 'int'> >>> c = str(i) >>> type(c) <type 'str'> >>> c '100' >>> exit()
以下に変換し変換後のクラスを表示するサンプルコードを記します。
i = 100 s = "200" c = str(i) print type(c) c = int(s) print type(c)
上記サンプルコードの実行結果を記します。
<type 'str'> <type 'int'>
以上、数値から文字列変換と数値のみの文字列から数値変換のサンプルコード等でした。