string format format

1. The format is followed by a tuple type! ! !

If there is no one-to-one correspondence, an error will be reported, such as one missing element to replace

Indexes can be added in parentheses, see example 2

You can use the index to take only one value, such as {1} in parentheses

r = "i am {}, i am {} yearsold".format("nana",18)
print(r)

i am nana, i am 18 yearsold
r = "i am {1}, i am {0} yearsold".format("nana",18)
print(r)

i am 18, i am nana yearsold

2. Pay attention to the parentheses behind, and use the symbols according to the format syntax

You can also use the form of a dictionary, but pay attention to the addition of * and square brackets, such as example 2

r = "i am {name}, i am {age} yearsold".format(name = "nana",age = 18)
print(r)

i am nana, i am 18 yearsold
r = "i am {name}, i am {age} yearsold".format(**{"name": "nana","age":18})
print(r)

i am nana, i am 18 yearsold

3.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340048&siteId=291194637