Python输入s=“tom 80 90”求s中数字的和
时间: 2024-05-11 16:19:50 浏览: 11
可以使用Python中的正则表达式模块`re`来匹配字符串中的数字,并将其累加求和。
下面是一个示例代码:
```python
import re
s = "tom 80 90"
# 使用正则表达式匹配s中的数字
numbers = re.findall(r'\d+', s)
# 将数字列表中的每个元素转换为整数并求和
total = sum(map(int, numbers))
print(total)
```
输出结果为:170
相关问题
python s=jion 80 70,求s中数字和
抱歉,您的问题不太清楚。如果您想让我计算字符串 "80 70" 中数字的和,可以使用以下代码:
```
s = "80 70"
numbers = s.split()
total = sum(int(num) for num in numbers)
print(total)
```
输出结果将为 150。
python s=“ion 80 70”,求s中数字和
可以通过字符串操作和循环来实现求解s中数字和的功能,具体代码如下:
```python
s = "ion 80 70"
total = 0
for char in s:
if char.isdigit():
total += int(char)
print(total)
```
输出结果为:15
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)