今回は、PythonのStringからIntへの変換とintからstringへの変換を理解します。
Pythonでは、暗黙のうちに値がタイプキャストされることはありません。
変数を明示的に型キャストする方法を確認しましょう。
1. Python String から int への変換
Python の int() メソッドを使うと、String 型の任意の値を整数の値に変換することができます。
構文は以下の通りです。
int(string_variable)
|
例えば、以下の様になります。
string_num = '75846'
print("The data type of the input variable is:)
print(type(string_num))
result = int(string_num)
print("The data type of the input value after conversion:)
print(type(result))
print("The converted variable from string to int:)
print(result)
|
結果は以下の通りです。
The data type of the input variable is:
<class 'str'>
The data type of the input value after conversion:
<class 'int'>
The converted variable from string to int:
75846 |
Pythonの文字列から基数の異なるint型への変換
Pythonでは、String型の数値/値を、数体系に従った特定の底値の整数値に変換する効率的なオプションも提供されています。
構文は以下の通りです。
int(string_value, base = val)
|
例えば、以下の様になります。
string_num = '100'
print("The data type of the input variable is:)
print(type(string_num))
print("Considering the input string number of base 8....")
result = int(string_num, base = 8)
print("The data type of the input value after conversion:)
print(type(result))
print("The converted variable from string(base 8) to int:)
print(result)
print("Considering the input string number of base 16....")
result = int(string_num, base = 16)
print("The data type of the input value after conversion:)
print(type(result))
print("The converted variable from string(base 16) to int:)
print(result)
|
上記のコードでは、’100’を底が8と底が16の整数値にそれぞれ変換している。
結果は以下の通りです。
The data type of the input variable is:
<class 'str'>
Considering the input string number of base 8....
The data type of the input value after conversion:
<class 'int'>
The converted variable from string(base 8) to int:
64Considering the input string number of base 16....
The data type of the input value after conversion:
<class 'int'>
The converted variable from string(base 16) to int:
256 |
この記事もチェック:Pythonのfloat関数を使って整数や文字列を浮動小数点に変換する方法
Python String to int 変換時の ValueError Exception
シナリオ:入力文字列のいずれかに、10進数に属さない桁が含まれている場合。
以下の例では、文字列 ‘A’ を基数16の整数値に変換したいのに、 int() メソッドの引数に base=16 を渡さなかった場合、 ValueError Exception が発生します。
なぜなら、’A’が16進数であっても、10進数には属さないので、int()関数の引数に base = 16 を渡さない限り、Aは10進数の10と等価とは見なされないからです。
例えば、以下の様になります。
string_num = 'A'
print("The data type of the input variable is:)
print(type(string_num))
result = int(string_num)
print(type(result))
print("The converted variable from string(base 16) to int:)
print(result)
|
結果は以下の通りです。
The data type of the input variable is:
<class 'str'>
Traceback (most recent call last): File "main.py", line 4, in <module>
result = int(string_num)
ValueError: invalid literal for int() with base 10: 'A'
|
Python の Integer のリストを String のリストに変換します。
Pythonの整数型リストは、リスト内包のint()メソッドを使って、文字列のリストに変換することができます。
例えば、以下の様になります。
st_lst = ['121', '144', '111', '564']
res_lst = [int(x) for x in st_lst]
print (res_lst)
|
結果は以下の通りです。
[121, 144, 111, 564]
|
この記事もチェック:PythonでリストをJSON文字列に変換する方法
2. Python int から String への変換
Python の str() メソッドを使うと、整数型の任意の値を文字列に変換することができます。
構文は以下の通りです。
str(integer_value)
|
例:
int_num = 100
print("The data type of the input variable is:)
print(type(int_num))
result = str(int_num)
print("The data type of the input value after conversion:)
print(type(result))
print("The converted variable from int to string:)
print(result)
|
結果は以下の通りです。
The data type of the input variable is:
<class 'int'>
The data type of the input value after conversion:
<class 'str'>
The converted variable from int to string:
100 |
この記事もチェック:Pythonのstrptimeを使って文字列を日付時刻に変換する方法
まとめ
今回は、Pythonの文字列から整数への変換、およびその逆を理解しました。
この記事もチェック:Pythonのchr関数とord関数を使って文字列と整数を変換する方法