DevNet Associate DEVASC 200-901 Official Certification Guide by Adrian Iliesiu (z-lib.org)

(andrew) #1
\r: Carriage return

You can add multiple arguments to the print() function
by using commas between elements. This is very useful
in creating meaningful text, and the print() function
also handles concatenation of the different data types
automatically. Consider this example:


Click here to view code image


>>> print('Numbers in set', 1, ':', numbs )
Numbers in set 1 : {1, 2, 4, 5, 6, 8, 10}

By default, the print() function uses a separator
between elements. This is normally not an issue if you
want spaces to appear between words or elements. In the
previous example, you can see a space between the 1 and
the : that just doesn’t look good. You can fix it by
changing the separator that the print() functions uses
with the sep=" attribute (using single quotes with
nothing in between). Since you will be removing all
automatic spacing, you have to compensate for this by
adding spaces in your actual text if you need them.
Remember that separators come between elements and
don’t add anything to the start or end of the print()
function. Consider this example:


Click here to view code image


>>> print('Numbers in set ', 1, ': ', numbs,
sep='' )
Numbers in set 1: {1, 2, 4, 5, 6, 8, 10}

One capability added to Python 3.6 and up is the
addition of f-string formatting. Not only are these strings
easier to read and less prone to syntax errors but they
allow you to write formatting code a lot faster. To create
an f-string, you put an f at the beginning of a string,
within the print() function, to let Python know what you

Free download pdf