python如何将'22'转化为int(22)
时间: 2023-06-09 13:03:02 浏览: 47
可以使用int()函数将字符串'22'转换为整数型数据22,示例代码如下:
```
num_str = '22'
num = int(num_str)
print(num)
```
输出结果为:
```
22
```
相关问题
python将numpy转化为int
可以使用numpy中的astype方法将numpy数组转换为int类型。具体来说,可以使用astype(int)将其转换为int类型。
例如:
```python
import numpy as np
arr = np.array([1.2, 2.5, 3.9])
int_arr = arr.astype(int)
print(int_arr)
```
输出结果为:
```
[1 2 3]
```
其中int_arr的数据类型为int。
python将float转化为int
可以使用内置函数int()将float转化为int。例如:
x = 3.14
y = int(x)
print(y) # 输出3
也可以使用内置函数round()将float四舍五入为int。例如:
x = 3.14
y = round(x)
print(y) # 输出3
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)