Printing to the console!

Sign up to access this lesson

Click here to sign up and get access to this lesson!

Saving Progress...

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 below to practice what you learn in this lesson:

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

This outputs:

This is my number 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,'-'))

This will output a line like this:

-----------------------------This is a printed line-----------------------------

Left and right adjusting

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

This outputs a line like this:

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

Tabbing Output

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

print("First name\t\tLast Name")

This will show the following:

First name              Last Name
Paul                    Hill
Joe                     Friday
Jonathan                Marston

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:

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

This outputs:

First name          Last Name
Paul                Hill
Joe                 Friday
Jonathan            Marston

Sign up to access the rest of this lesson

You must either log in or sign up to access this lesson.

Saving Progress...

0 0 votes
Lesson Rating
Notify of
Inline Feedbacks
View all comments