All Courses
Python Input and Output

Python Input and Output

May 17th, 2019 python

Python Input and Output version2

Some of the functions like input() and print() are widely used for standard input and output operations respectively. There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use.

Input:

The input stems from the keyboard. For this purpose, Python provides the function input(). input has an optional parameter, which is the prompt string. To allow flexibility we might want to take the input from the user. In Python, we have the input() function to allow this.
The syntax for input() is:

Syntax

input([prompt])

Example:

num = input('Enter a value: ')
#user entered 10
o/p => Enter a value: 10
print(num)
o/p=> 10

raw_input()

does not interpret the input. It always returns the input of the user without changes, i.e. raw. The
syntax for raw_input() is:

Syntax:

raw_input([prompt])

Example:

age = raw_input("Your age? ")
#user entered 20
o/p=> Your age? 20
print(age)
o/p=> 20

Python Input and Output version3:

Some of the functions like input() and print() are widely used for standard input and output operations respectively. There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use.

Input:

One way to get data is directly from the user. The value of variables were defined or hard coded into the source code. To allow flexibility we might want to take the input from the user. In Python, we have the input() function to allow this.
The syntax for input() is:

Syntax:

input([prompt])

Example:

name = input('Enter a name: ')
#user entered vinod
o/p => Enter a name: vinod
print(name)
o/p=> ‘vinod’

Output:

The print statement becomes the print() function in Python 3.0. Making print() a function makes it possible to replace the function by doing def print() or importing a new function from somewhere else. The syntax for input() is:

Syntax:

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

Example:

print_test.py
print(1,2,3,4,sep='*')
# Output: 1*2*3*4
print(1,2,3,4,sep='#',end='&')
# Output: 1#2#3#4&

Output:

We use the print() function to output data to the standard output device (screen). The syntax of the print() function is:

Syntax:

print(*args, sep=' ', end='\n', file=None)

The parameters are:

  • args: positional arguments whose values will be printed out.
  • sep: the separator, which will be printed between arguments.
  • end: the ending text, which will be printed after all of the arguments have been output.
  • file: the file object to which the output will be sent.

Note:

Python 2.6 has a __future__ import that removes print as language syntax, letting you use the functional form instead.

Example:

from __future__ import print_function
print('# of entries', len(dictionary), file=sys.stderr)

Example:

ptint_test.py
a=10
b=”hi”
c=”10.20”
print(“this is a print test”, a, b, c)
#o/p => this is a print test 10 hi 10.20
Social Share:

0 responses on "Python Input and Output"

Leave a Message

Your email address will not be published. Required fields are marked *