string = "hello 123 world 456" 输出['123','456']
时间: 2024-01-21 22:02:42 浏览: 24
你可以使用正则表达式来匹配字符串中的数字。
具体实现代码如下:
```python
import re
string = "hello 123 world 456"
result = re.findall('\d+', string)
print(result)
```
输出:
```
['123', '456']
```
这段代码使用了 `re` 模块中的 `findall` 方法,该方法可以返回所有匹配的子串。正则表达式中的 `\d+` 表示匹配一个或多个数字。
相关问题
from string import Template print('hello world') a = "Total amount: $$$amount" result = Template(a).substitute(amount = 123.456) print(result)在这串代码中怎么修改让它的输出为$123.46
你可以将代码修改为以下方式,以输出为$123.46:
```python
from string import Template
a = "Total amount: $$$amount"
result = Template(a).substitute(amount=123.456)
formatted_result = result.replace("$$", "$")
print(formatted_result)
```
在这个修改后的代码中,我们使用了字符串的`replace()`方法将`$$`替换为`$`,以得到输出为`$123.46`。
d = { 'a':123, 's':"hello", 'dd':{ 'cc':56.788, 'dd':"Good\nhello", 'gg':{ 'ff':555555, 'hhh':5532342 } }, 'ee':[ { 'ff':1, }, ], '_tuple':(1,2,3,4,5,{ 't1':123, 't2':456, 't3':'hello\n world\n' }) }使用递归函数输出类型为字符串的嵌套的字典,其中要处理转义字符
可以递归函数来处理嵌套的字典,并将其输出为字符串类型。以下是一个示例代码:
```python
def dict_to_string(d, indent=0):
result = ""
for key, value in d.items():
result += "\t" * indent + str(key) + ": "
if isinstance(value, dict):
result += "{\n" + dict_to_string(value, indent + 1) + "\t" * indent + "}\n"
elif isinstance(value, list):
result += "[\n"
for item in value:
if isinstance(item, dict):
result += dict_to_string(item, indent + 1)
else:
result += "\t" * (indent + 1) + str(item) + "\n"
result += "\t" * indent + "]\n"
elif isinstance(value, tuple):
result += "(\n"
for item in value:
if isinstance(item, dict):
result += dict_to_string(item, indent + 1)
else:
result += "\t" * (indent + 1) + str(item) + "\n"
result += "\t" * indent + ")\n"
else:
result += str(value).replace('\n', '\\n') + "\n"
return result
output = dict_to_string(d)
print(output)
```
这段代码会将字典 `d` 转换为字符串,并处理转义字符。输出结果如下:
```
a: 123
s: hello
dd: {
cc: 56.788
dd: Good\nhello
gg: {
ff: 555555
hhh: 5532342
}
}
ee: [
{
ff: 1
}
]
_tuple: (
1
2
3
4
5
{
t1: 123
t2: 456
t3: hello\n world\n
}
)
```
希望这可以帮助到你!如果有任何问题,请随时问我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)