+ 1

format function in Python.

Can anyone please explain me about the format function in Python

10th Jul 2019, 4:00 PM
Yash
Yash - avatar
3 Respostas
10th Jul 2019, 4:32 PM
Lothar
Lothar - avatar
+ 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?
10th Jul 2019, 4:35 PM
Seb TheS
Seb TheS - avatar