Simply preprocess your template. For example, if you want [[...]] to be your template markers and leave single { and } characters alone:template = """ <script> function work() { alert('bla'); } </script> Success rate is [[value:.2%]] percent."""result = template.replace("{", "{{").replace("}", "}}").replace("[[", "{").replace("]]", "}").format(value=0.8675309)试图用不同数量的大括号来完成这一切是很棘手的,所以我肯定会使用其他一些字符.但是请注意,像 [[ 和 ]] 之类的东西可能会合理地出现在合法的 JavaScript 代码中.使用永远无法使用的东西可能会更好.Trying to do it all with varying numbers of curly brackets is tricksy, so I would definitely use some other characters. Careful, though, things like [[ and ]] could reasonably occur in legitimate JavaScript code. Might be better to use something that never could. 这篇关于带有自定义占位符的高级 Python 字符串格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-30 08:21