Featured Post

How To Use Python Arithmetic Operators

Image
How To Use Python Arithmetic Operators Introduction Arithmetic Operators are used in Python to carry out mathematical operations on numerical values. It is very important to understand this operator well in order to complete Python projects that require you to do carry out mathematical calculations. In this guide, you will come across all the different types of arithmetic operators that will be explained in an easy-to-understanad manner. Types of Python Arithmetic Operators There are six types of arithmetic operators in Python and in this guide, I will be explaining every one of them with detailed explanation as well as with simple example exercises, for your easy understanding! So, keep reading this post until the very end , so that you do not miss out any valid points. The 6 types of arithmetic operators are :  1. Addition Operator  2. Subtraction Operator   3. Multiplication Operator  4. Division Operator 5. Modulus Operator  6. Exponential Operator Addi...

How To Use Python Print Function

How To Use Python Print Function




Introduction


This Python beginner guide will explain you the basics of using the Python print() function as it is the most frequently used function in Python examples, excercises and also Python project. The print function helps us in displaying the output on the computer screen.  

Printing numbers

When you print numbers, be it an integer or a float, Python printing is quite simple, all you need to do is to use the print() function and write the required number inside the parenthesis. Look at the code given below:

print(70)
print(70.50)
In both the above cases, we will be able to display the integer 70 and the float 70.50 on the screen as the output. All you need to type is just write the integer or float number inside the parenthesis of the print() function and you can get to see the output right away on the display screen of your code editor.

Printing Booleans

As you know already, booleans such as True or False statements are very important in Python functions.  If you want to display something as True , simply write the boolean True inside the print function parenthesis and if you want to display something as False, just write the bookean False, inside the print function parenthesis,  The outcome for both the above cases would be  True and False respectively .


print(True)
#output True
print(False)
#output False

The output will be obviously True and False on the display, respectively. It is as simple as that! As we all know, True and False are booleans in Python to state if the provided expressions in python are True or False . 


Printing Variable Values


In cases where you have created some variables and you want to print out a statement and address it to the user, it is very easy and convenient to do so with the help of the print function. Let us look at a simple example below:

name = "edtech by meera"
print("Thanks for watching", name)
print("Thanks for watching" + name)
#output: Thanks for watching, edtech by meera
In the above example, we have created a variable called, name and assigned it a value of "edtech by meera" . Next, we want to thank the user, that is, the person with the value of name variable, in this case, so how do we do this? We use the print() function and our first parameter is the string statement for greeting the person , in this case, "Thanks for watching" and please note, it has to be in the form of a string always, and it has to be followed by a comma and following the comma, you should write the variable name, (just write the variable name, not it's value!).  The variable name in this case behaves as a seat holder for being substituted by the value of the variable after using the print function. The displayed message would be : Thanks for watching, edtech by meera

There is another way of doing the above example, that is, instead of using the comma after the string sentence of greetings, you may just use the  '+'   symbol , followed by the variable name. This process is called concantenation whereby you are adding the string "Thanks for watching" with the name vaariable, with a value of 'edtech by meera' The output will be the same as in the previous method even though the methods are slightly different.

Printing multiple variables


Consider variables, date with a value of 26, month with a value of 11 and year with a value of 2025. and you can print all these variables simultaneously. How to do it? All you need to do is use the print function and write the different variables in the order sequence inside the parenthesis of the print function. So, here inside the print function, we have multiple parameters to print out the date, month, and year respectively, as shown below.



CODE 

date = 26
month = 11
year = 2025
print(date,month,year, sep = "-")

#outcome 26-11-2025

Here you will note the usage of the character , "-" and this is referred to as a separator, as we need a separator to separate the digits for the date, month and year respectively. Based on the separator, we can display our date, month and year accordingly! For example, we can write, 26-11-2025, or  26.11.2025 or 26/11/2025

For getting the "." or the "/" we need to write those as a separator in the print statement accordingly.


Printing string statements with new lines

You can print out a line in different line after a previous line, that is, a new line, by using the 'forward slash followed by n' that is, '\n' as shown in the code below. If you use the new line , your next line will jump to the next line below as shown below.


CODE 

print('Hello \n World!'
#outcome : Hello
           World!

As you can see in the above code, when you  print out a simple string statement such as a welcome message but you want to display in different lines, then, you can use the \n ( forward slash with n stands for new line) and this will create a new line after the previous line. In the above case, the outcome will be as shown above in the source code.


This is because of the new line that you created in between the first line and the second line.


Printing with separators


In the below code, you see an example where we have used a 'sep', short for, a separator, with a value of \n\n and you will observe that by using the \n twice, you will create a double space between the different lines.

We have used another parameter inside the print function, called , 'end' and the value that you have allocated to the end parameter is four new lines by writing four times the new line character. So, after the string in the output, there will be four new lines .


The output ends after the four new line gaps at the end of the string output.

In the below code, where we have used the separator value as "|" and the end value as four new lines 
"\n\n\n\n" 


Hence the output will be as shown below:

CODE

print('Mary','Peter', 'Tom', 'Harry', sep='\n\n',end = '\n\n\n\n')
#The output will be:
Mary,
Peter,
Tom,
Harry

print('Mary', 'Peter','Jon', sep = "|", end = '\n\n\n\n')
#output: Mary|Peter|Jon|


Output ends with a gap of four new lines at the end of the line where the names are indicated in the output but the three names are separated by the vertical slash as shown above.



Printing Multiple Lines


If you need to print out multiple lines in one go, such as a paragraph or  more than two or three paragraphs, then the following way you have to do the print function.


CODE
print('''
this is a test this to test ipdum lorrn ipdum loren
ipsum loren ipsum loren 
ipdum loren loren loren ipdum ipdum
''')



You need to write a pair of  triple single quote or a pair of  triple double quotes as shown above.
Triple quote for single quote looks like ''' ''' and triple quote pair for double quote looks like """ """ and inside the pair of the triple quotes , we insert the required paragraphs as shown above inside the print function. In the above print function, I have used triple single quotes as shown above., that is, ''' '''


Printing strings using percentage operator


CODE 
my_color = "black"
print("my favorite color is %s" %my_color)


In the above example, we create a variable called my_color and assign it a value of  "black"  and then we use the print function to print out that "my favorite color is black" .
How to do it?
Inside the print function, parenthesis, the first parameter is the string , "my favorite color is" and this is followed by the percentage operator, %s as the first parameter, that is, the first parameter is "my favorite color is %s"

Note that the percentage operator % is followed immediately by the character s , because for writing the percentage operator with a string statment, we need to write %s as we are writing it with a string arameter. In this case, you will notice that there is no comma after the first parameter. After a space, we again repeat the percentage operator, % followed by the variable name my_color.

So, to write the entire print function with the parameters, it would be :


CODE
print("my favorite color is %s" %my_color)


Print numbers using percentage operator

Earlier, we had seen how simple it is to print out numbers, that is , integers and floats, using a print function by mentioning the integer or float , respectively,  inside the parenthesis of the print function, and here, we will observe how to print them using the % operator as well . We have already seen above, how to print strings using the percentage operator, now, let us see how to print out integers and floats using the percentage character.

CODE
number = 25
print("my favorite number is %d" %number)

In the above case, we follow the same code syntax as with the above example, but, the only difference is that , instead of % followed by s in the case of a string, that is, instead of using %s for string statements as the example above, we use %d in the below case to print out digits , as shown below.
Rest of the code syntax structure remains  the same as in the above example.


We created a variable called number and assigned it a value of 25 and then we print by writing the string "my favorite number is %d"   %number

Here, the percentage operator acts as a placeholder to insert the value of the respective variables provided and we use %d where d stands for digits when we print out digit values.



Printing using F- Strings



CODE
print (f"This color is blue")      
  #outcome:  This color is blue    


This is done by just putting the alphabet f before the string literal. In the below case, by writing f before the string :this color is blue", we end up writing like the way in our source code above.
We can easily get the outcome of the string as shown above in the source code. This method of using f- string  comes in very handy when there are several strings to print out or when you need to print out a complex string statement.


Printing from Files

In my previous blog post, I had mentioned and explained how to open and close files for file manipulation, while working with files in Python. In this blog post, you will learn how to print something by referring to a particular file that you have uploaded to your code editor. All you need to do is to first open your file using the Python open() function as shown in the code below, that is also having a write parameter inside the open function. Next, you need to write the print() function with the first parameter being the string that you want to print out and display on the computer and the second parameter being the reference to the file that you wanted to use for this purpose. Lastly, you need to close the specific file that you requested to be opened and this will close the file and end the code session here. Please take a close look at the code below to understand what I have explained here.


CODE 

my_file = open('lesson.txt','w')
print('the file has my lesson plans', file = my_file)
my_file.close()


Printing using the Python print format method


In string format method, we need to have two parameters inside the format parenthesis. In the below example, we have created a variable called pet and assigned it a value of 'Gigu' and another variable called age and assigned it a value of 15

CODE 
pet = "Gigu"
age = 15
print("Pet:{}, Age: {}".format(pet,age))
#Outcome is : Pet: Gigu, Age: 15



Inside the format method parenthesis, whatever order we write the variables, we get the outcome in that particular order . In the above example, pet is followed by the age variable inside the format parenthesis. So in the output, we have something like shown above in the code above.


In the format method as shown above, we use the curly braces to act as placeholders for the relevant values to fill up the respective variables. 

Inside the print function parenthesis, we first write the string "Pet followed by a colon and then the curly braces {} followed by a comma and then the next string  Age followed by a colon which in turn is followed by a pair of curly braces {} that acts as a placeholder to fill in the values of the variable , age, followed by the closing quotes " 

Then we use the .format() which is the format method with the two variables , pet and age mentioned inside it's parenthesis.



Printing using the F string and Curly braces

CODE 
print(f"Pet:{pet},Age:{age}")     #outcome Pet: Gigu, Age: 15

Consider the above example, where we use the F string and curly braces to print out the pet name and age on the display.


Here, we first write the string f followed by the open quote " and then the string statement with colons followed by the curly braces, but here the curly braces are not empty like in the previous example , but the curly braces are also holding the name of the respective variable names as shown above.

The outcome will be same as in the previous example.


Print Using percentage operator and multiple variables

In the below example, we write the string with colon followed by the percentage operator %s  followed by a comma and then one more string for the age with colon followed by the percentage operator %d followed by the parameters , pet and age in that order inside the print parenthesis. 

The outcome will still be the sae as in the above two examples shown in the above two scenarios. Please try them out in your code editor and let me know if it worked for you.

If any errors, please mention in the comment section below the post and I will be only too glad to be of help!


CODE 
print("Pet:%s,Age:%d" %(pet,age))


Conclusion

In this blog post, we learnt the essentials of print function used in all of the Python projects and example cases. The print function is used to print out the required items to the console so that the outcome of the code could be displayed on the screen to the user after he or she runs the program. It is very essential not only for displaying the outcome to the user but also for debugging the program , because, after running the program, once you check the console screen for the outcome, you will also be able to read the bug or error in your code  and these bugs would be clearly mentioned on the display in your console once you run the program after using the print command. As you are able to see the error messages after running the print function, you are able to check your code again and debug the code. So, you see now, how important is your print function, especially for code beginners since we are more prone to errors in our beginning coding journey, especially in Python as even a simple indentation error can lead to several bugs in your code!

We also learnt the different methods of using the Print function, using f-strings, curly braces, percentage operator, string formatting, etc. We also learnt how to give new lines in our print outcomes and use various types of separators in our print function to give specific format to our printed result. We learnt how to print numbers, strings and how to print out multiple variables and multiple string lines and paragraphs, and not to forget, how to use print using uploaded files.

If you have not understood any part of this post, do mention in the comment section below.




More Python related posts for Python beginners:










Comments

Popular Posts

Build A Random Quote Machine in React

Build A Calculator in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Guide to Learn Coding Efficiently and Effectively