# 数値と文字列の演算 ###### tags: `Python` | 関数 | 説明 | | -------- | -------- | | int(x) | 文字列を数値に変換するにはint()を使います。 | | float(x) | 文字列を浮動小数点数に変換するにはfloat()を使います。 | ## 数値と文字列の演算 Python では数値と文字列を直接演算することはできません。 ```python= n = input("n: ") print(n + 3) ``` ``` TypeError: can only concatenate str (not "int") to str ``` 数値と文字列の演算を行う場合には、文字列をいったん数値に変換する必要があります。 ### int() 文字列を数値に変換するにはint()を使います。 **Example:** ```python= n = input("n: ") print(int(n) + 3) ``` ### float() 文字列を数値に変換するにはfloat()を使います。 **Example:** ```python= m = float(input("Enter a float number : ")) print(m + 1.1) ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up