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 }}.
一个简单的例子是:
name = "Tom"
print("Hello, {}".format(name)) # prints "Hello, Tom"
"First, thou shalt count to {0}" # References first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"From {} to {}" # Same as "From {0} to {1}"
"My quest is {name}" # References keyword argument 'name'
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
{}
是替换字段,而不是dict
。这在Python Documentation中提到:一个简单的例子是:
下面是来自Python Documentation的更多示例:
这是替换字段的语法:
相关问题 更多 >
编程相关推荐