In Python, we have two widely used methods for taking user input. First input() method and second, readline() method defined as sys.stdin.readline(). I would like to know if there are any situations when input() method is preferred over readline() method.
As I understand, readline() is faster than input(). Both of these return string value and we have to typecast them according to our needs. There is also a readlines() method to read user inputs on multiple lines. Is it better to use readlines() when reading multiple lines from user?
input() method does not require any import, where as readline() requires an import of sys.stdin. Still input() is slower. Does this mean time required for importing is negligible?
TLDR Speed is not a factor.
If your program is designed to read data from standard input, the like of a Unix filter, you won't use input but you will use the methods of sys.stdin, exactly because you have different methods to match the data flow of your program.
On the other hand, if your program is designed to interact with a user then input is clearly the way to go. Because the speed of the program is not the infinitesimal difference in speed of the two calls but the speed of the user interaction and the extra convenience provided by the optional prompt string is a very strong argument in favour of input.
Is there any situation when input() is preferred over sys.stdin.readline()?