Complicated formatting of a string in Python

PHOTO EMBED

Sun Jun 07 2020 05:51:39 GMT+0000 (Coordinated Universal Time)

Saved by @Amna #python

formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4,))
print(formatter.format(one, two, three, four))
print(formatter.format(true, false, false, true))
content_copyCOPY

In this, we use a function to turn the formatter variable into other strings. When we write formatter.format(...), it means: 1. Take the formatter string defined in line 1. 2. Call its format function, which is similar to telling it to do a command-line command named format. 3. Pass to format arguments, which match up with the four { } in the formatter variable. This is like passing arguments to the command line command format. 4. The result of calling format on formatter is a new string that has the { } replaced with the four variables. This is what print is now printing out.

https://www.amazon.com/Learn-Python-Hard-Way-Introduction/dp/0134692888/ref=pd_lpo_14_img_0/145-2038954-9524128?_encoding=UTF8&pd_rd_i=0134692888&pd_rd_r=2c1aff3f-60bb-490b-a2d0-dfc06d81970a&pd_rd_w=t9wy6&pd_rd_wg=Bmxlp&pf_rd_p=7b36d496-f366-4631-94d3-61b87b52511b&pf_rd_r=RTCDC9M99JAV7NJ30VVE&psc=1&refRID=RTCDC9M99JAV7NJ30VVE