Python Input Methods for Competitive Programming
                                        
                                                                                    
                                                
                                                    Last Updated : 
                                                    17 Sep, 2025
                                                
                                                 
                                                 
                                             
                                                                             
                                                             
                            
                            
                                                                                    
                Python is generally slower than C++ or Java, but choosing the right input method helps reduce time and avoid errors. Below, we’ll look at the most common input techniques in Python, when to use them, and their pros and cons.
Example Problem 
Let’s consider a problem: Find the sum of N numbers entered by the user.
Example
Input:   5
              1 2 3 4 5
Output:  15
We’ll now explore different input methods in Python that can help solve such problems efficiently.
Below are the main ways to take input in Python, Let’s go through them one by one.
1. Normal Method 
This is the simplest and most commonly used method using input() for taking input and print() for displaying output.
- input() always reads input as a string, so we need to typecast it (e.g., to int).
- For arrays/lists, we can use split() and map() to convert them into integers.
- It works fine for small inputs, but it is slow for large inputs, which is why competitive programmers prefer other methods.
Example: This code takes n as the number of inputs, then reads n integers, sums them up and prints the result.
            Python
    n = int(input())
arr = [int(x) for x in input().split()]
summation = 0
for x in arr:
    summation += x
print(summation)
Explanation:
- first line n = int(input()) takes the number of elements.
- second line reads the elements as space-separated integers.
- loop calculates the sum and finally, print() displays the result.
2. Faster Method Using stdin and stdout 
Python’s sys.stdin and sys.stdout are much faster than input() and print().
- stdin.readline() reads input directly from the input buffer.
- stdout.write() outputs results faster because it avoids the formatting overhead of print().
This method is widely used in competitive programming for speed optimization.
Example: This program reads input using stdin.readline(), computes the sum of numbers and prints it using stdout.write().
            Python
    from sys import stdin, stdout 
def main():
    n = int(stdin.readline())
    arr = [int(x) for x in stdin.readline().split()]
    summation = sum(arr)
    stdout.write(str(summation))
if __name__ == "__main__":
    main()
Explanation:
- stdin.readline() reads input as raw text much faster.
- stdout.write() avoids extra formatting overhead of print().
Timing comparison (100k lines each):
- print(): 6.040s
- Writing to file: 0.122s
- stdout.write(): 0.121s
Clearly, stdin + stdout is much faster.
Sometimes, we need to unpack multiple values directly into separate variables. For example, if input is:
5 7 19 20
We want:
a = 5
b = 7
c = 19
d = 20
Instead of writing parsing code repeatedly, we can create a helper function.
Example: This function reads a line of space-separated integers and unpacks them into variables.
            Python
    import sys
def get_ints(): 
    return map(int, sys.stdin.readline().strip().split())
a, b, c, d = get_ints()
Explanation: get_ints() uses map(int, split()) to convert input into integers. The values are directly assigned to variables in one line.
When you want the entire line of numbers stored in a list, you can use another helper function. For example, input:
1 2 3 4 5 6 7 8
We want:
Arr = [1, 2, 3, 4, 5, 6, 7, 8]
Example: This program reads integers from a single line and stores them in a list.
            Python
    import sys
def get_list(): 
    return list(map(int, sys.stdin.readline().strip().split()))
Arr = get_list()
Explanation: map(int, split()) converts all numbers to integers. Wrapping it in list() gives us a proper list. Now Arr holds all the integers as a list.
Sometimes, instead of numbers, we just need a string input. For example:
GeeksforGeeks is the best platform to practice Coding.
We want:
string = "GeeksforGeeks if the best platform to practice coding."
Example: This function reads a line of text input as a string.
            Python
    import sys
def get_string(): 
    return sys.stdin.readline().strip()
string = get_string()
Explanation: sys.stdin.readline() reads the entire line. .strip() removes trailing newlines. The result is stored as a plain string.
6. Adding a buffered pipe io (Python 2.7) 
When inputs/outputs are very large, even sys.stdin may not be enough. In such cases, we can use a buffered pipe (io.BytesIO) to further speed up I/O. This is more advanced and usually required only for problems with huge input sizes (2MB+ files).
Example: This program uses a buffer to speed up output operations.
            Python
    import atexit, io, sys
# A buffer for output
buffer = io.BytesIO()
sys.stdout = buffer
# Print via buffer
@atexit.register
def write():
    sys.__stdout__.write(buffer.getvalue())
#####################################
# Example program
n = int(input())
arr = [int(x) for x in input().split()]
summation = sum(arr)
print(summation)
Explanation: Output is written into an in-memory buffer instead of directly printing. At program exit, atexit.register writes all data at once. This reduces the overhead of multiple print() calls.
                                
                                
                            
                                                                                
                                                            
                                                    
                                                
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        Complete CP Guide
Basics
Bit manipulation
DP for CP
Advanced