How can we format output with print (that is, control what gets printed out between separate arguments, and what gets printed out after the actual value)? →
We can use special arguments, called keyword arguments, to control output with print. sep="separator" and end="ending".
Let's check out some examples in the next slide. →
Formatting With Print Examples
note that there are no quotes around sep and end
these keyword arguments go after regular arguments
The format() Function
The format() function can be used to format a value before you use it elsewhere (for example, before you print out to the user)
it takes two arguments
the thing (value) you want formatted (can be a string or numeric type)
some formatting pattern (expressed as a string)
format() always returns a string
the original value is not modified in any way!
Padding Strings
You can use format to ensure that you string has some number of characters in it.
to fill out the remaining characters, format will add spaces before or after your original string
this is specified by the format string
for example, '>20s' and '<20s' add spaces to the beginning and end of a string…
Examples of Formatting Floats
Specifying places after decimal (. means decimal, number is the number of spaces, f means floating point format)
Examples of Formatting Integers
Specifying places after decimal (, means add comma, d means integer)
The Formal Specification
Ok… so if you want to know how format really works:
the second argument is a string that represents a format
and it's written in a "mini-language" (yes, a language in a language!)