Complicated formatting of a string in Python


Sun Jun 07 2020 05:51:39 GMT+0000 (UTC)

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))

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.