AppDividend
Latest Code Tutorials

Python print() Function Example

0

If we want to something to the standard output device ( like Monitor ), then the most straightforward way in Python is to use the print() function. Python print() function converts an object that you want to print to the screen into a string, and then it prints that object.

Python print()

The print() function prints the given object to the standard output device (screen) or the text stream file.

See the following syntax.

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

There are some parameters as we can see on the above syntax:

  • Object: Object is that thing which is to be printed. There may be many objects in a single print() function. That’s why we have used * before an object.
  • sep: This sep means the objects which to be printed can be separated by a separator like a comma(,), space (‘ ‘), etc. However, that default value of sep is ‘ ‘.
  • end: This specifies what to be printed at the end of the statement. By default, the end value is ‘\n’ (next line).
  • file: This must be an object with the write method. The default value is sys.stdout, which means the output will be printed on the screen.
  • flush: This is a boolean value if the value is True, the stream is forcibly flushed. The default value is False.

However, only the object parameter is required, and the other three parameters are optional.

The function does not have any return value.

See the following example.

# app.py

s = "Hello welcome to AppDividend"
# This will print the value of object s
print(s)

num = 10
# Usages of sep
# This will print the message and the value by a space
print("Value of the number is", num, sep=' ')

# Usages of sep and end
num1 = 20
print("Value of num is", num, sep=' ', end="\n\n")
print("Value of num1 is", num1, sep=' ')

Output

➜  pyt python3 app.py
Hello welcome to AppDividend
Value of the number is 10
Value of num is 10

Value of num1 is 20
➜  pyt

Here on the above code, we can see that when we are using sep = ‘ ‘ , it separates two objects by a space and then printing it.

Also, we can see, we have used end=’\n\n’, which means there will be two next line gaps between two print statements as per the above python code.

Python print() with file parameter

See the following code.

# app.py

# Opening a file in write mode
myfile = open('demo.txt', 'w')
# This message will be written to the file
msg = "Hello there"

# Printing the message to demo.txt
print(msg, file=myfile)

# Closing the file
myfile.close()

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

Here, we have passed the myfile file object to the file parameter. The string object ‘Hello there’ is printed to the demo.txt file (check it in your system).

So, we have opened a file name demo.txt, and if this file does not exist in the current directory where the source code file is located, firstly, it will be created.

After that we the message “Hello there” will be printed ( in write mode) to the file.

If you do not know how to read and write a file in Python, then check out these two tutorials.

  1. How to read a file in Python
  2. How to write a file in Python

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

Related Posts

Python repr()

Python property()

Python pow()

Python iter()

Python next()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.