关于format问题
>>> "{{1}}".format("不打印", "打印")'{1}'
这个的结果为什么是 {1}? 想不明白,哪位大神解释一下 help(format)
Help on built-in function format in module __builtin__:
format(...)
format(value[, format_spec]) -> string
Returns value.__format__(format_spec)
format_spec defaults to ""
"{1}".format("不打印", "打印") 输出是 打印
"{0}".format("不打印", "打印") 输出是 不打印 两个引号相当于对引号的转义,即{{1}}变为了直接打印{1},而不是起{1}占位符的作用。 @冬雪学冬。还是没明白,能再解释一下么????{:10_258:} {n}是占位符(n=0,1,2...),语句"{1}".format("不打印", "打印") 表示用“打印”替换{1},如果就是想打印{1},则必须写成{{1}} {:10_256:}{:10_256:}{:10_256:}我的困惑也没了 这个视频里面讲过啊~就是相当于用大括号对大括号进行了一个转义,就像用斜杠对斜杠转义一样。 无下限HENTAI 发表于 2016-4-7 18:31
这个视频里面讲过啊~就是相当于用大括号对大括号进行了一个转义,就像用斜杠对斜杠转义一样。
就是说format()里面的参数都没有用了,因为只打印前面{}转义里面的内容. 使用help('FORMATTING')就知道怎么回事了。
Format strings contain “replacement fields” surrounded by curly braces
"{}". Anything that is not contained in braces is considered literal
text, which is copied unchanged to the output.
If you need to include a brace character in the literal text, it can be escaped by doubling:
"{{" and "}}".
页:
[1]