Python Tutorial
What is Python ??
Python Get Started
Python Syntax
Python Variables and Types
Python Data Types
Python Operators
Python Input and Output
Python If ... Else
Python While Loops
Python For Loops
Python Strings
Python Functions
Python Lambda Function
Author Profile Altaf Shaikh
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Python Input and Output

January 17th 2020

Python Input and Output
Python Input and Output

The print() function

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

Note: The print() is a function in Python 3. You should use parentheses around your parameter.

The full syntax of print() is:

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

print() Parameters

  • objects - object to the printed. * indicates that there may be more than one object
  • sep - objects are separated by sep. Default value' '
  • end - end is printed at last
  • file - must be an object with write(string) method. If omitted it, sys.stdout will be used which prints objects on the screen.
  • flush - If True, the stream is forcibly flushed. Default value: False

Note: sependfile and flush are keyword arguments. If you want to use sep argument, you have to use:

print(*objects, sep = 'separator')

not

print(*objects, 'separator')

 

Return Value from print()

It doesn't return any value; returns None.

Example 1: How print() works in Python?

In the above program, only objects parameter is passed to print() function (in all three print statements).

Hence,

  • ' ' separator is used. Notice, the space between two objects in output.
  • end parameter '\n' (newline character) is used. Notice, each print statement displays the output in the new line.
  • file is sys.stdout. The output is printed on the screen.
  • flush is False. The stream is not forcibly flushed.

The end parameter of print function

Removes the newline at the end of the print function

The sep parameter of print function

objects are separated by sep. Default value' '

Example 2: print() with separator and end parameters

The file parameter of print function

In Python, you can print objects to the file by specifying the file parameter.

Example 3: print() with file parameter

In Python, you can print objects to the file by specifying the file parameter.


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

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

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

 

The input() function

input() function is used to take input in python.

Note: input() function takes the input in the string format. Convert it into suitable format for different use.

Example 3: input() function

 

Command Line Arguments

  • In order to pass the command-line arguments, copy the following code in 'cmd_args.py'
  • Execute 'cmd_args.py' in terminal using the command '$ python3 cmd_args.py arg1 arg2 arg3'
# Filename: cmd_args.py
import sys
print(sys.argv)

# OUTPUT:
# $ python3 cmd_args.py arg1 arg2 arg3
# ['cmd_args.py', 'arg1', 'arg2', 'arg3']