Skip to content

Python input enter pressed | Example code

  • by

Create a While True loop that calls for input until the input differs from the empty string to check input enter pressed or not in Python.

You can also use a simple if statement with the input value. If Text is empty then it means enter has been pressed by the user.

Example get know user input enter pressed in Python

Simple example code check user will hit enter and you will check if it was entered or not.

guess = ""

while True:
    if guess == "":
        guess = input("Please Enter a letter! ")
    else:
        break

print(guess)

Output:

Python input enter pressed

Detect Enter has pressed

text = input("type in enter")  # or raw_input in python2
if text == "":
    print("you pressed enter")
else:
    print("you typed some text before pressing enter")

Output:

type in enter
you pressed enter

Do comment if you have any doubts or 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.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading