fish_python 发表于 2016-4-1 09:23:49

关于format问题

>>> "{{1}}".format("不打印", "打印")
'{1}'

这个的结果为什么是 {1}? 想不明白,哪位大神解释一下

凌志 发表于 2016-4-1 10:05:08

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 ""

shuofxz 发表于 2016-4-1 10:07:20

"{1}".format("不打印", "打印")       输出是 打印
"{0}".format("不打印", "打印")       输出是 不打印

冬雪雪冬 发表于 2016-4-1 10:07:54

两个引号相当于对引号的转义,即{{1}}变为了直接打印{1},而不是起{1}占位符的作用。

fish_python 发表于 2016-4-1 11:30:18

@冬雪学冬。还是没明白,能再解释一下么????{:10_258:}

冬雪雪冬 发表于 2016-4-1 11:39:24

{n}是占位符(n=0,1,2...),语句"{1}".format("不打印", "打印") 表示用“打印”替换{1},如果就是想打印{1},则必须写成{{1}}

doudou_freefish 发表于 2016-4-7 15:12:13

{:10_256:}{:10_256:}{:10_256:}我的困惑也没了

无下限HENTAI 发表于 2016-4-7 18:31:03

这个视频里面讲过啊~就是相当于用大括号对大括号进行了一个转义,就像用斜杠对斜杠转义一样。

cheney8899 发表于 2017-5-25 12:59:28

无下限HENTAI 发表于 2016-4-7 18:31
这个视频里面讲过啊~就是相当于用大括号对大括号进行了一个转义,就像用斜杠对斜杠转义一样。

就是说format()里面的参数都没有用了,因为只打印前面{}转义里面的内容.

成亟亟 发表于 2020-4-21 17:46:01

使用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]
查看完整版本: 关于format问题