Level 1
0 / 100 XP

Printing to the console!

Printing output in Python is something you will need to do quite often to understand things are going in your python program. It can be used for debugging, show progress or just to display information that may be important to whomever is running the script.

Use the Python console on the right to practice what you learn in this lesson.

Interactive Python Console

We can start by using print in its most simple form:

print("This is a printed line")

Use the percent sign to substitute

We can use the %s operator to replace text inside of our string:

print("This is my number %s" %(25))

Center Method

You can use the center method to add even padding on either side of your python:

print("This is a printed line".center(80,'-'))

Left and right adjusting

Left:

print("This is a printed line".ljust(80,'-'))

Right:

print("This is a printed line".rjust(80,'-'))

Tabbing Output

If we escape the t by preceding it with a backslash (like: \t) we can insert tabs into our output:

Python
print("First name\\t\\tLast Name") print("Paul\\t\\t\\tHill") print("Joe\\t\\t\\tFriday") print("Jonathan\\t\\tMarston")

This isn't optimate because you will notice that we need to modify the number of tabs based on how many characters are in our output.

Column Alignment with %

We can also use the % method to format our print statements:

Python
print("First name Last Name") print("%-*s %s" % (19, 'Paul', 'Hill')) print("%-*s %s" % (19, 'Joe', 'Friday')) print("%-*s %s" % (19, 'Jonathan', 'Marston'))