首页 > 代码库 > str.format()

str.format()

Format strings contain “replacement fields” surrounded by curly braces {}

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

Two conversion flags are currently supported: ‘!s‘ which calls str() on the value, and ‘!r‘ which calls repr().

str.format()