Hello everyone! I'm here again with another interesting lesson on Python programming. And today I will be emphasizing on the print function. So Let's get started.
Printing in python:
The print function is a command we use to instruct the program to print/display an output. Here is an example.
The print function as you can see requires parenthesis around it's argument. And as you can see from the code above the argument is the string Hello Python. In Python anything inside the quotes, with a few exceptions, will be printed out exactly as it appears.
Don't worry about the exceptions as I'll be treating it in this article. But before then let's see the difference between integers in quotes vs intergers without quotes. What you should understand is that anything written in quotes using the print function is interpreted by the computer as a string, and the computer will display it exactly as written in the code. And on the other hand intergers written without quotes is interpreted by the computer as a problem that needs to be evaluated. Here is a simple example.
The above statement will print 5 + 7.
The above statement will print 12.
We also use a comma to print several things at once. Python will automatically insert spaces between them. Here is an example and the output it produces.
The print function have two optional Arguments, the sep and the end. They are not very important, but they can be used to make your outputs look nice.
Python will automatically insert spaces between each of the arguments of the print function. But there is an optional argument calledsep, you cause to change that space to something else. For example using sep=':' would separate the argument by a colon.
One good way of using this is to have nothing inside quotes as in sep=''. This instructs the program to put no spaces within the Arguments. Below is an example of how to make your output nice.
The print function automatically advances to the next line. For example, the following will print two lines.
But you can use the optional argument called end to prevent the print function from advancing to the next line. Here is an example.
Of course you could achieve this better with a single print, but as time goes on you will see the importance of the end argument.
Exercise to try:
- Ask a user to enter a number x. Use the sep optional argument to print out x, 2x, 3x, 4x and 5x. Each separated by three dashes like below.
Please ensure you try all the codes in this article for a better understanding. And also attempt all the exercises as well. If you are confused or stuck anywhere fell free to comment below and I or any other programmer in the house will give you a hint.
Thanks for reading patiently! I hope you enjoyed it? Please share your class experience with us. And know that your ideas to make this class a success is also appreciated, just feel free 🙂
My most recent programming articles.
- Solutions to my exercises on Python Programming (Part 1).
- Python programming tutorial 1–(Installing Python, getting user Inputs and running basic programs)
- My new Steemit project and how I intend to run it.
- Top 3 reasons why everyone should learn programming.
If you miss any of my tutorial don't worry as you can always visit my page to see all my articles, or click on the #edywill tag to see all my recent programming articles.
To get more programing tutorials, tips and technological updates you won't want to miss out, subscribe to my Steemit page by following @edywill.