Python Format string "}"填充

标签 python string-formatting

Python 2.6 定义了 str.format(…) ,从 Python 3.0 开始,它优于旧的 % 字符串格式样式。然而,查看“mini-language”,似乎无法使用字符作为填充字符。

这对我来说似乎是一个奇怪的遗漏。是否有可能以某种方式转义该字符并将其用作填充字符?

我知道我可以填充一些独特的字符串字符,然后使用 str.replace 或任何数量的其他类似技巧来获得相同的结果,但这感觉就像破解我...

编辑:我所追求的是这样的

>>> "The word is {0:}<10}".format("spam")
'The word is spam}}}}}}'

最佳答案

您始终可以将其指定为单独的参数,如下所示:

>>> "The word is {0:{1}<10}".format("spam", "}")
'The word is spam}}}}}}'

看,它被传入并用于代替 {1}。

关于Python Format string "}"填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1945230/

相关文章:

python - 在 Python 中解析上下文无关文法

python - 正则表达式匹配未用数字括起来的 5 位子字符串

c++ - 在 C++ 中拆分 const char*

c# - 将十进制值格式化为具有 2 个小数位的货币

c# - 如何从字符串中删除 DateTime 子字符串

python - igraph python 导入错误

python - 检测我如何运行 Python 脚本

python - 使用 asyncio 协同程序进行方法链接

python - 将格式化文本存储在变量 python 中

C: 如何让 fgets 从文件中读取一个\n 换行符?