Python print() Function

In this tutorial you will learn about python print() function, which is used to print objects and also about variables and parameters of the print function.

Python print() Function

PYTHON PRINT() FUNCTION

The print() function prints the given message to the screen of the output device, which can be a python interpreter, Terminal, or an IDE (integrated development environment), and the message can be a string or any other object like a list, tuple, set and dictionary.

Syntax of print() function

print(object(s), sep=separator, end=end, file=file, flush=flush)

Python PRINT() FUNCTION PARAMETERS

  • Object: will print any object.
  • sep: will separate objects in more than one way. (Optional)
  • end: will specify what to print at the end. (Optional)
  • file: will print an object with a write method. (Optional)
  • flush: A Boolean specifying if the output is flushed (True) or buffered (False). (Optional)

Now let see some examples of python print() function.

Example 1: How to print a string in python

print("Hello World!")

The output will be as follows.

Hello World!

Example 2: How to print multiple string in python.

print("Hello World!", 'We are using Python3.')

Output:

Hello World! We are using Python3.

Python PRINT() FUNCTION WITH SEP PARAMETER

The sep parameter stands for a separator which is used to separate the output of an object. The default value in the sep parameter is ‘  ‘.

Example: How to use sep parameter with print function.

print("String1",'String2','String3')
print("String1",'String2','String3', sep = '/')
print("String1",'String2','String3', sep = '-')
print("String1",'String2','String3', sep = '0')
print("String1",'String2','String3', sep = '#')

When we run the above program, we will get the following output.

String1 String2 String3
String1/String2/String3
String1-String2-String3
String10String20String3
String1#String2#String3

As we can see, we are using different symbols in a sep parameter, and it is replacing space with that specific symbol we used in the sep parameter.

Python PRINT() FUNCTION WITH END PARAMETER

This parameter is used to specify the end of the printing object.

Example: Python print variable.

print("Hello World", end='!')
print("Python3 is Awesome",end=';')

And the output will be as follows.

Hello World!
Python3 is Awesome;

Example: print() Function with file Parameter

In python, how can python print a file and print the content inside a file.

For better understanding, read Python File I/O.

my_file = open('python.txt', 'w')
print('Hello World!', file = my_file)
my_file.close()

This program tries to open the python.txt in writing mode. If this file doesn’t exist, the python.txt file is created and opened in writing mode.

Here, we have passed the my_file file object to the file parameter. The string object ‘Pretty cool, huh!’ is printed to the python.txt file (check it in your system).

Finally, the file is closed using the close() method.

Example: Python print to Print a formatted string:

name = "Alice"
age = 25
print("My name is {0} and I am {1} years old.".format(name, age))

Output:

My name is Khus and I am 25 years old.

Example: how to print a list in python?

fruits = ["apple", "banana", "cherry"]
print(fruits)

Output:

['apple', 'banana', 'cherry']

Example : How to print a python dictionary.

person = {"name": "Bob", "age": 30}
print(person)

Output:

{'name': 'Bob', 'age': 30}

Conclusion

In conclusion, the print() function is an essential tool for displaying output in Python. It is a simple yet powerful function that allows you to print variables, strings, and other objects to the console or a file. By using the print() function, you can quickly and easily debug your code, provide feedback to the user, or generate reports and logs.

The print() function offers several parameters that give you control over the output, such as the separator between values, the character at the end of the output, the output stream, and whether to flush the buffer. Additionally, you can use string formatting and the * operator to format and print complex data structures.

Whether you’re a beginner or an experienced Python developer, mastering the print() function is essential. It is one of the most frequently used functions in Python programming and is a building block for many other more advanced techniques. By understanding the basics of print(), you can effectively communicate with the user, debug your code, and generate useful output for your projects.

FAQs

How do you use the print function in Python?

The basic syntax for using the print function in Python is as follows:

print("Hello, World!")

This will display the string “Hello, World!” on the console.

What are the arguments of the print function in Python?

The print function in Python can take one or more arguments separated by commas. The arguments can be of different data types such as strings, integers, and floats.

Can you print multiple values with the print function in Python?

Yes, you can print multiple values with the print function in Python by separating the values with commas. For example:

print("The answer is", 42)

This will display “The answer is 42” on the console.

How do you print variables with the print function in Python?

You can print variables with the print function in Python by using the variable name as an argument.
For example:

name = "John"
print("My name is", name)

his will display “My name is John” on the console.

How do you format the output of the print function in Python?

You can format the output of the print function in Python by using special characters such as %s and %d. For example:

name = "John"
age = 30
print("My name is %s and I am %d years old" % (name, age))

This will display “My name is John and I am 30 years old” on the console.

How do you redirect the output of the print function in Python?

You can redirect the output of the print function in Python by using the “>” character followed by the filename. For example:

print("Hello, World!", file=open("output.txt", "w"))

This will redirect the output of the print function to a file named “output.txt”.

How do you print to the console and a file at the same time with the print function in Python?

You can print to the console and a file at the same time with the print function in Python by using the “tee” function from the “contextlib” module. For example:

from contextlib import redirect_stdout

with open(“output.txt”, “w”) as f:
with redirect_stdout(f):
print(“Hello, World!”)

This will display “Hello, World!” on the console and redirect the output to a file named “output.txt”.