Open In App

Difference between input() and sys.stdin.readline()

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a widely used general-purpose language that can be used for many purposes. Taking input in any language is as important as breathing for humans. Python provides various methods for taking input. However, we all may get confused about how each method is different from one another. In this article, we will discuss about two such methods i.e input() and sys.stdin.readline().

Note: For more information, refer to Python Tutorial

Input()

This function first takes the input from the user and then evaluates the expression, which means Python automatically identifies whether the user entered a string or a number or list. If the input provided is not correct then either syntax error or exception is raised by Python.

How the input function works in Python :

  • When input() function executes program flow will be stopped until the user has given input.
  • The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional.
  • Whatever you enter as input, input function converts it into a string. if you enter an integer value still input() function converts it into a string. You need to explicitly convert it into an integer in your code using typecasting.

Example:




# Program to check input  
# type in Python 
    
num = input ("Enter number :"
print(num) 
name1 = input("Enter name : "
print(name1) 
    
# Printing type of input value 
print ("type of number", type(num)) 
print ("type of name", type(name1))


Output:

python-input

Sys.stdin.readline()

Stdin stands for standard input which is a stream from which the program reads its input data. This method is slightly different from the input() method as it also reads the escape character entered by the user. More this method also provides the parameter for the size i.e. how many characters it can read at a time.

Example:




# Python program to demonstrate
# sys.stdin.readline()
  
  
import sys
  
name = sys.stdin.readline()
print(name)
  
num = sys.stdin.readline(2)
print(num)


Output:

python-stdin

Difference between Input and sys.stdin.readline() function.

Input() sys.stdin.readline()
The input takes input from the user but does not read escape character. The readline() also takes input from the user but also reads the escape character.
It has a prompt that represents the default value before the user input. Readline has a parameter named size, Which is a non-negative number, it actually defines the bytes to be read.


Previous Article
Next Article

Similar Reads

Take input from stdin in Python
In this article, we will read How to take input from stdin in Python. There are a number of ways in which we can take input from stdin in Python. sys.stdininput()fileinput.input()Read Input From stdin in Python using sys.stdin First we need to import sys module. sys.stdin can be used to get input from the command line directly. It used is for stand
2 min read
Python exit commands: quit(), exit(), sys.exit() and os._exit()
The functions quit(), exit(), sys.exit(), and os._exit() have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exits and no stack traceback is printed. We can catch the exception to intercept early exits and perform cleanup activities; if uncaught, the interpreter exits as usual. In this article,
4 min read
Uses of OS and Sys in Python
In this article, we will see where we use Os and Sys in Python with the help of code examples. What is Os Module?Python OS module in Python furnishes a versatile means of engaging with the operating system. It facilitates a range of operations, including the creation, deletion, renaming, movement, and modification of attributes for files and direct
4 min read
Python - sys.settrace()
Python sys module provides some of the powerful functions but they are complex to understand. One of which is the sys.settrace() which is used for implementing debuggers, profilers and coverage tools. This is thread-specific and must register the trace using threading.settrace(). Knowing how the function works internally is really important in case
3 min read
Python | sys.getallocatedblocks() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getallocatedblocks() is used to get number of memo
1 min read
Python | sys.getrecursionlimit() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getrecursionlimit() method is used to find the cur
1 min read
Python | sys.getdefaultencoding() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getdefaultencoding() method is used to get the cur
1 min read
Python | sys.setrecursionlimit() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.setrecursionlimit() method is used to set the maxi
2 min read
Python | sys.setswitchinterval() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.setswitchinterval() method is used to set the inte
2 min read
Python | sys.getswitchinterval() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getswitchinterval() method is used to get the inte
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg