DEV Community

Cover image for Everyting you need to know about the Print() function in Python.
Ivaylo Ivanov
Ivaylo Ivanov

Posted on

Everyting you need to know about the Print() function in Python.

Python - a high-level scripting language, with very readable syntax and many outstanding built-in functions. Hi, guys in this series we are going to review the Python built-in functions - how to use them, their special keywords, and best use cases. Without further due, let's dive into the topic.

Today we are going to look into the print function. This function is highly used by almost every Python developer. Its main purpose is to print out some data to the console.

1. More arguments than one.

As you probably know, the print function can take more than one argument. For example, you can print out two strings separated by a comma.

print("Hello", "Python")
Enter fullscreen mode Exit fullscreen mode

You can put as many arguments as you want in the print function, just separate them with a comma.

print(arg1, arg2, arg3, arg4, arg5)
Enter fullscreen mode Exit fullscreen mode

Now that you know how to use print with multiply arguments, there are some options that you can set within the print function.

2. Sep option.

The sep option is used to tell the print function, how to separate multiply arguments when it prints them out.

print("Hello", "Python", "Hello", "World" sep="-")
Enter fullscreen mode Exit fullscreen mode

Expected Output: Hello-Python-Hello-World
You can put anything in the sep keyword.

3. File option.

The option is used to tell Python where to print out the output of the print function. By default, it is printed out in the console, but if you want, you can tell the print function to save the output in a file.
To do this, you have to create a variable where you are going to set the name of the file and the type of writing in it - w or a.
w - the file content is going to be overwritten(delete its old content first and then write the current output)
a - the file content is not going to be deleted, just the current output is going to be appended to the file.

output = open("output.txt", "a")

print("Hello Python", file=output)
Enter fullscreen mode Exit fullscreen mode

Now the output is saved in the file output.txt.

4. End option.

This option is how every print line is ending. By default, it ends with \n - new line. We can change that by using the end keyword.

print("Python is awesome", end="!!!")
Enter fullscreen mode Exit fullscreen mode

Expected Output: Python is awesome!!!
As you can see the end of the printed line has changed. You can use the end keyword for formatting the output nicely.

5. Supported versions.

After you have learned about the important use and options of the print function, it is time to determine which version of python supports these options.

All of the above is supported by python3.X, but a new keyword was introduced in python3.3 by PEP421. The name of the new keyword is Flush. You can read more about it here.

Congratulations, you just deep-dived into the print function and learned how to use it and its options.

If you have any questions, leave a comment below. I will answer it as soon as possible.

Top comments (0)