Skip to content

Python while input is not empty | Example code

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.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.

1 thought on “Python while input is not empty | Example code”

  1. This does not work in Python 3 it seems. This is my code:

    def detalles_cliente(prompt):
    while True:
    return(input(prompt))
    if prompt:
    return(prompt)
    break

    NOMBRE_CLIENTE = str(detalles_cliente(‘Nombre Cliente: ‘))
    print(NOMBRE_CLIENTE)

    Even if you enter nothing it breaks out with no errors.

Leave a Reply

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