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:
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:
No comments yet. Add the first comment to start the discussion.