3 ответов
+ 4
You can find everything here: (use also the second link!)
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2455/
https://realpython.com/python-string-formatting/
+ 2
Ofcourse.
format is a string method, which is used to insert values to string.
You can actually do the same thing with string concatenation, but using format method:
-You do not need to convert nonstring values to strings.
-Using format method can be more readable.
I will describe few main features of the format method, but it actually has much more features.
You can insert values in place of {}s in order you give arguments to the format method:
print("{} {} {}".format(1, True, "yesterday"))
#Output: 1 True yesterday
You can put numbers in {} to change the order of the passed values:
print("{2} {1} {0}".format(1, True, "yesterday"))
#Output: yesterday True 1
You can use keyword arguments to increase readability:
print("My name is {name}".format(name="... I don't know what my name is."))
#Output: My name is ... I don't know what my name is.
print("{x} + {y} = {z}".format(x=5, y=9, z="3?"))
#Output: 5 + 9 = 3?