Categories: python

Input and Output in Python

Two built-in functions in Python are used to perform input and output operations (OI Operations). The two built-in functions for performing output and input operations are listed below.

  • print() – This function is used to output data.
  • input() – This function is used to perform input operations.
Take Input from the User:

To perform input operations, Python includes the function input(). The input() function can be used both with and without a message.

Syntax:

input('prompt')

where prompt is an optional string displayed on the string when taking input.

Example:

# Taking input from the user
name = input("Enter your Name: ")

print("Hello, " + name)
print(type(name))

Output:

Enter your Name: Rabecca Fatima
Hello, Rabecca Fatima
<class 'str'>

By default, Python treats all input as a string. To convert it to another data type, we must explicitly convert the input. To convert an int or a float, for example, use the int() and float() methods, respectively.

Example:

# Taking input from the user
Roll = int(input("Enter your Roll: "))

print( Roll)
print(type(Roll))

Output:

Enter your Roll: 20012
20012
<class 'int'>
Output Operation using print() :

The built-in function print( ) is used to output the given data to the standard output device (Screen). When we use the print() function to display a message, the string can be enclosed in single or double quotations.

Note: the return value of print( ) function is None.

Syntax:

print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)

Parameters:

  • value(s) : Any value, and as many as you like. Will be converted to a string before getting printed
  • sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
  • end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
  • file : (Optional) An object with a write method. Default :sys.stdout
  • flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False
  • Returns: It returns output to the screen.

Example:


Roll = 102

print( "ROLL NUMBER :", Roll)

Output:

ROLL NUMBER : 102
Using % Operator:

We can implement the ‘%’ operator. % values are replaced with zero or more element values. % formatting is similar to ‘printf’ formatting in the C programming language.

  • %d is an integer,
  • %f is a float,
  • %s is a string,
  • %x is hexadecimal.
  • %o stands for octal.

Example:

# Taking input from the user
num = int(input("Enter a value: "))

multi = num *11

# Output
print("The product is %d" %multi)

Output:

Enter a value: 9
The product is 99

Note: also read about Modules and Functions in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago