Print Formatting in Python
Print Formatting in Python
/
Print Formatting.ipynb (/github/jmportilla/Complete-Python-Bootcamp/tree/master/Print Formatting.ipynb)
Print Formatting
In this lecture we will briefly cover the various ways to format your print statements. As you code more and
more, you will probably want to have print statements that can take in a variable into a printed string
statement.
In [17]:
This is a string
Strings
You can use the %s to format strings into your print statements.
In [4]:
s = 'STRING'
print 'Place another string with a mod and s: %s' %(s)
In [12]:
In [13]:
In [15]:
In [19]:
In [23]:
In [24]:
Multiple Formatting
Pass a tuple to the modulo symbol to place multiple formats in your print statements:
In [22]:
In [27]:
In [28]:
# Multiple times:
print 'One: {p}, Two: {p}, Three: {p}'.format(p='Hi!')
In [29]:
# Several Objects:
print 'Object 1: {a}, Object 2: {b}, Object 3: {c}'.format(a=1,b='two',c=12.3)
That is the basics of string formatting! Remember that Python 3 uses a print() function, not the print
statement!