Python while input is not empty | Example code

  • by

You have to use a break statement if the user enters some value. Where the while loop will run and prompt the input message until some value is given by the user. The syntax for while input is not empty in Python.

while True:

    s = input()

    if not s:
        break
    somelist.append(s)

You can check input is empty or not with the help of the if statement.

x=input()
if x:
    print(x)
else:
    print('empty input')

Run while loop input is not empty in Python

Simple example code.

name = ''

# Start a loop that will run until the user give input
while True:
    name = input("Enter Name to exist: ")

    if name:
        print(name)
        break

Output:

Python while input is not empty

Do comment if you have any doubts and suggestions on this Python input topic.

Note: IDE: PyCharm 2021.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.