0

How to check if input is empty in python?

While s = "" ? How to check if the input I receive is empty using only the while command.

11th Nov 2018, 2:36 AM
Eddy
Eddy - avatar
6 Respuestas
+ 6
You can check input is empty or not with the help of if statement. x=input() if x: print(x) else: print('empty input')
11th Nov 2018, 4:41 AM
Maninder $ingh
Maninder $ingh - avatar
+ 2
A tip: when you are dealing with EOF or something alike always remember that, to python, every null or empty or zero it's managed like a "false"... So you can put something like "while s:" because, if it's null or empty, will be false to python
11th Nov 2018, 2:59 AM
Eduard Arias
Eduard Arias - avatar
+ 2
One pattern would be: s = input() while s: somelist.append(s) s = input() It's a bit annoying to write the same line twice, but there is no 'do while' like in c. Or you use this: while True: s = input() if not s: break somelist.append(s)
11th Nov 2018, 9:04 AM
HonFu
HonFu - avatar
+ 2
input() returns a string. To check whether it is empty, just check its length. s=input() while len(s)==0: s=input()
12th Nov 2018, 10:49 AM
Mayur Garg
Mayur Garg - avatar
+ 1
You might want to use the catch exception method. Whenever you enter an input, in case that input is empty, it produces an End of File Error written as EOFError. So your code should look something like this, try: #enter input s = input() #do something with your input except EOFError: #action when input is empty or there is no input The code above should do it. ;)
2nd Apr 2021, 1:02 PM
Mahlangu Nzunda
Mahlangu Nzunda - avatar
0
x=input() if not x: print('empty input')
6th Aug 2021, 9:19 AM
Aske