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:

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:

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

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 for Beginners

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete