String percent formatting

1. You can concatenate strings by + sign

r = "d af dafad"+"df dsfad dfa"
print(r)、

d af dafaddf dsfad dfa

2. Concatenate strings through percent signs, splice them to the specified position, add parentheses when passing multiple values, any element can be replaced, such as tuples, numbers, etc.

print("i am %s father"%"your")

i am your father
v = "i am %s my %s"%("your","father")
print(v)

i am your my father

3. %d can only accept numbers, but only using %s is less readable

4. Print floating-point numbers. The number before f represents the number of digits after the print point. The point after the percent sign corresponds to the point to be replaced later, which can be rounded up

 

r = "precent %.2f"%99.261515
print(r)

precent 99.26

 

5. To print a percentage, just add two percent signs after it

r = "precent %.2f %%" % 997.2121
print(r)

precent 997.21 %

6. Dictionary replacement, note that the following parentheses are square brackets

r = "i am %(name)s i am %(age)s  " % {"name":"long","age":21}
print(r)

i am long i am 21  

7. Add stuff between lists

print("root","dfdf","dfad",sep=":")

root:dfdf:dfad

8. Sao operation is not commonly used

 

Guess you like

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