Open In App

Different Input and Output Techniques in Python3

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

An article describing basic Input and output techniques that we use while coding in python.

Input Techniques

1. Taking input using input() function -> this function by default takes string as input. 

Example:

Python3




#For string
str = input()
 
# For integers
n = int(input())
 
# For floating or decimal numbers
n = float(input())


2. Taking Multiple Inputs: Multiple inputs in Python can be taken with the help of the map() and split() methods. The split() method splits the space-separated inputs and returns an iterable whereas when this function is used with the map() function it can convert the inputs to float and int accordingly.

Example:

Python3




# For Strings
x, y = input().split()
 
# For integers and floating point
# numbers
m, n = map(int, input().split())
m, n = map(float, input().split())


3. Taking input as a list or tuple: For this, the split() and map() functions can be used. As these functions return an iterable we can convert the given iterable to the list, tuple, or set accordingly.

Example:

Python3




# For Input - 4 5 6 1 56 21
# (Space separated inputs)
n = list(map(int, input().split()))
print(n)


Output:

[4, 5, 6, 1, 56, 21]

4. Taking Fixed and variable numbers of input: 

Python3




# Input: geeksforgeeks 2 0 2 0
str, *lst = input().split()
lst = list(map(int, lst))
 
print(str, lst)


Output:

geeksforgeeks [2, 0, 2, 0]

Output Techniques

1. Output on a different line: print() method is used in python for printing to the console.

Example:

Python3




lst = ['geeks', 'for', 'geeks']
 
for i in lst:
    print(i)


Output:

geeks
for
geeks

2. Output on the same line: end parameter in Python can be used to print on the same line.

Example 1:

Python3




lst = ['geeks', 'for', 'geeks']
 
for i in lst:
    print(i, end='')


Output:

geeksforgeeks

Example 2: Printing with space.

Python3




lst = ['geeks', 'for', 'geeks']
 
for i in lst:
    print(i,end=' ')


Output:

geeks for geeks

3. Output Formatting: If you want to format your output then you can do it with {} and format() function. {} is a placeholder for a variable that is provided in the format() like we have %d in C programming.

Example:

Python3




print('I love {}'.format('geeksforgeeks.'))
 
print("I love {0} {1}".format('Python', 'programming.')


Output:

I love geeksforgeeks.
I love Python programming.

Note: For Formatting the integers or floating numbers the original method can be used in the {}. like ‘{%5.2f}’ or with the numbers, we can write it as ‘{0:5.2f}’. We can also use the string module ‘%’ operator to format our output.

4. Output using f strings: 

Python3




val = "Hello"
print(f"{val} World!!!")




Previous Article
Next Article

Similar Reads

Variations in different Sorting techniques in Python
These are all different types for sorting techniques that behave very differently. Let's study which technique works how and which one to use. Let 'a' be a numpy array a.sort() (i) Sorts the array in-place & returns None (ii) Return type is None (iii) Occupies less space. No copy created as it directly sorts the original array (iv) Faster than
3 min read
Python3 Program to Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]<=arr[j] if i is odd and j < i
Given an array of n elements. Our task is to write a program to rearrange the array such that elements at even positions are greater than all elements before it and elements at odd positions are less than all elements before it.Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 7} Output : 4 5 3 6 2 7 1 Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8} Output : 4
3 min read
SciPy - Input and Output
In this article, we learn about SciPy input and output. In order to handle inputs and outputs of multiple formats, Scipy.io package provides multiple methods. Some of the formats that can be handled by the Scipy.io package are: MatlabNetcdfIDLArffMatrix MarketWave Among this Matlab is the format which is used very frequently. Now let's see the func
2 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, you can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Python Basic Input and OutputIn this introductory guide, we'll explore the essentials of Python's
6 min read
Python3 Program for Check if an array is sorted and rotated
Given an array of N distinct integers. The task is to write a program to check if this array is sorted and rotated counter-clockwise. A sorted array is not considered as sorted and rotated, i.e., there should at least one rotation.Examples: Input : arr[] = { 3, 4, 5, 1, 2 } Output : YES The above array is sorted and rotated. Sorted array: {1, 2, 3,
3 min read
Python2 vs Python3 | Syntax and performance Comparison
Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using Python 3.x from now-onwards. Still in dilemma? Ev
4 min read
How to install Python3 and PIP on Godaddy Server?
GoDaddy VPS is a shared server that provides computational services, databases, storage space, automated weekly backups, 99% uptime, and much more. It’s a cheaper alternative to some other popular cloud-based services such as AWS, GPC, and Azure. Python is an open-source, cross-platform, high-level, general-purpose programming language created by G
2 min read
How to create an instance of a Metaclass that run on both Python2 and Python3?
Metaclasses are classes that generate other classes. It is an efficient tool for class verification, to prevent sub class from inheriting certain class function, and dynamic generation of classes. Here we will discuss how to create an instance of a Metaclass that runs on both Python 2 and Python 3. Before delving into it, let's go through the code
3 min read
Python3 Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String
Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S. Examples: Input: S = "1001"Output: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0; at the end = 0. Sum= 0 + 0 = 0."0011": Count of 0
5 min read
Python3 Program to Minimize characters to be changed to make the left and right rotation of a string same
Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = “abcd”Output: 2Explanation:String after the left shift: “bcda”String after the right shift: “dabc”Changing the character at position 3 to 'a' and c
3 min read
Article Tags :
Practice Tags :