目次
(確認環境:Python 3)
int型を文字列にする
a = 100
print(type(a)) #<class 'int'>
b = str(a)
print(type(b)) #<class 'str'
print(b) # 100
strで文字列に変換しています。
float型を文字列にする
a = 100.25
print(type(a)) #<class 'float'>
b = str(a)
print(type(b)) #<class 'str'>
print(b) # 100.25
strで文字列に変換しています。
文字列をint型にする
a = '100'
print(type(a)) #<class 'str'>
b = int(a)
print(type(b)) #<class 'int'>
print(b) #100
# マイナスの場合
c = '-100'
print(type(c)) #<class 'str'>
d = int(c)
print(type(d)) #<class 'int'>
print(d) #-100
intでint型に変換しています。
文字列をfloat型にする
a = '100.25'
print(type(a)) #<class 'str'>
b = float(a)
print(type(b)) #<class 'float'>
print(b) #100.25
floatでfloat型に変換しています。
関連の記事
