CBSE CS

cbse cs logo

Home

Input / Output in Python

Input / Output in Python

  In Python, input/output (I/O) refers to the way a program interacts with the user and the outside world. There are several ways to perform input/output operations in Python, some of which are:
  1. input() function: This function is used to read input from the user through the console. It takes an optional string argument that serves as the prompt for the user to enter input. Example: name = input(‘Enter your name: ‘)
  2. print() function: This function is used to output text to the console. It takes one or more arguments that are concatenated and printed to the console. Example: print(‘Hello’, name)
  3. Reading and writing files: Python has built-in functions for reading and writing files, which allow you to work with external files on your computer.

input() method

 

Programs often need to obtain data from the user, usually by way of input from the keyboard. The simplest way to accomplish this in Python is with input().

Syntax for input() is:

data = Input (“Optional prompt”)

If prompt is present, it is displayed on monitor, after which the user can provide data from keyboard.

input() pauses program execution to allow the user to type in a line of input from the keyboard. Once the user presses the Enter key, all characters typed are read and returned as a string:

>>> name = input(‘What is your name? ‘)

What is your name? Winston Smith

>>> name

‘Winston Smith’

Output : print()

 

Output is what program produces. In algorithm, it was represented by print. For output in Python we use print.

Print Statement

Syntax:

print( expression/constant/variable)

Print evaluates the expression before printing it on the monitor. Print statement outputs an entire (complete) line and then goes to next line for subsequent output (s). To print more than one item on a single line, comma (,) may be used.

Example

>>>print( 3.14159* 7**2 )

>>>print( “I”, “am” + “class XI”, “student”)

>>>print( “I”m”, )

>>>print( “class XI student” )

>>>print( “I”m “, 16, “years old” )

Keyword Arguments to print()

print() takes a few additional arguments that provide modest control over the format of the output. Each of these is a special type of argument called a keyword argument.

Point to remember:

Keyword arguments have the form <keyword>=<value>.

Any keyword arguments passed to print() must come at the end, after the list of objects to display.

Keywords:

sep

end

 

 

 

The sep= Keyword Argument

 

Adding the keyword argument sep=<str> causes objects to be separated by the
string <str> instead of the default single space:

>>> print(‘foo’, 42, ‘bar’)

foo 42 bar

>>> print(‘foo’, 42, ‘bar’, sep=’/’)

foo/42/bar

>>> print(‘foo’, 42, ‘bar’, sep=’ ‘)

foo 42 bar

>>> d = {‘foo’: 1, ‘bar’: 2, ‘baz’: 3}

>>> for k, v in d.items():

print(k, v, sep=’ -> ‘)

foo -> 1

bar -> 2

baz -> 3

 

 

 

 

The end= Keyword Argument

 

The keyword argument end=<str> causes output to be terminated by <str> instead of the default newline:

if True:

print(‘foo’, end=’/’)

print(42, end=’/’)

print(‘bar’)

OUTPUT:

foo/42/bar

 

error: Content is protected !!