2024-04-27 00:34:28 发布
网友
我只是尝试在包含许多随机大括号的文本上使用Python的.format()。因为.format()试图替换单个大括号内的所有内容,所以它没有起作用。读了一些书之后,我似乎有三个不好的选择:
.format()
%
最好的选择是什么?还有更好的选择吗?在
这里有一个简单的方法:
>>> my_string = "Here come the braces : {a{b}c}d{e}f" >>> additional_content = " : {}" >>> additional_content = additional_content.format(42) >>> my_string += additional_content >>> my_string 'Here come the braces : {a{b}c}d{e}f : 42'
此外,还可以创建一个将大括号加倍的函数:
输出:
>>> Here come the braces : {a{b}c}d{e}f : 42
这里有一个简单的方法:
此外,还可以创建一个将大括号加倍的函数:
^{pr2}$输出:
相关问题 更多 >
编程相关推荐