Different Input and Output Techniques in Python3


Input and Output are the important operations to be performed in the programming language which allows the users to interact with program. Input implies the data or information provided to the program from the external source. Output is the way we display the processed data or information generated by the program to the given input data. There are different techniques in python for using the input and output, let’s see them one by one.

Different types of Input Techniques

The following are the input techniques that we can use to pass the input to the python program.

Standard Input

The standard Input is referred to take the input through the keyboard from the user. We will use the input() function available in python to take the input from the user. The following is the syntax.

variable = input()

Where,

  • variable is the variable name.

  • input() is the function to take the input from user.

Example

In the following example, we will use the input() function to take the dynamic input from the user.

a = 900
print("The integer value:",a)

Output

The integer value: 900

Command line Arguments

By using the Command Line Arguments, we can pass the input to the program by using the sys module of python programming.

Example

Here we will pass the inputs using the python script and access them using the sys module. Following is the lines of code mentioned in the python script.

import sys
print('Number of arguments:', len(sys.argv), 'arguments')
print('Argument List:', str(sys.argv))

To access inputs from the script, execute the following lines of code in the command prompt.

python augmented_script.py arg1 arg2 arg3

Output

Number of arguments: 4 arguments.
Argument List: ['augmented_script.py', 'arg1', 'arg2', 'arg3']  	

File Input

The file input technique referred to read the inputs from the python file. Here in this technique, we will use the open() function to open the file and to read the file input contents.

Example

In this example, we will create the python file by giving the input contents and read the inputs from the file.

Following are the inputs given in the python file.

a = 10
b = "python"
c = "Tutorialspoint"
with open('inputs.txt', 'r') as f:
   lines = f.readlines()
   print(lines) 

Output

['a = 10\n', 'b = "python"\n', 'c = "Tutorialspoint"']

Output Techniques

The following are the output techniques that we can use to display the output from the python program to the given input.

Standard Output

By using the Standard output technique we will print output to the console, with the help of the print() function available in python.

Example

In this example we will print the processed output from the program using the print() function.

a = "Welcome to Tutorialspoint"
print("The output:",a)

Output

The output: Welcome to Tutorialspoint

File output

The output can be written to a text file using the open() function in python.

Example

Here the output file will be created by using the open() function and writes the output of the inputs passed to the python program into the file.

with open('output.txt', 'w') as f:
   f.write("Hello, Welcome to Tutorialspoint. Have a happy learning")

Output

Hello, Welcome to Tutorialspoint. Have a happy learning

Formatted Output

Formatted output used to print the output in the specific format using the format() function available in python.

Example

Following is the example to get the defined format of output.

language = "Python Programming"
website = "Tutorialspoint"
print("The best website to learn {} is {} ".format(language, website))

Output

The best website to learn Python Programming is Tutorialspoint 

Updated on: 20-Oct-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements