My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Post hidden from Hashnode

Posts can be hidden from Hashnode network for various reasons. Contact the moderators for more details.

Python Getting User Input

Python Getting User Input

Edward Nyirenda Jr's photo
Edward Nyirenda Jr
·Mar 16, 2022·

2 min read

Getting user input is an essential part of any computer system. For every computer program to be useful, it must accept input in one way or another. There is a lot of input devices, but the most common is the keyboard, and mouse. Without any input from computer users, computers do not do anything, but just sit there idly.

In this article we discuss getting user input in Python. Let's see how we could prompt whoever is running our program for their name, so we could welcome them, or it could be anything besides their name.

Python comes with a built in function called input, built specifically for that problem. The input function reads a string from standard input, which is the keyboard and optionally accepts a prompt text. A prompt text says something like, "What is your name?", "How old are you?" et cetera.

if __name__ == "__main__":

    name = input("What is your name?")

    print("Hello,", name)

The above program prompts the user for their name, and waits for them to type it on the keyboard, and once they press enter, whatever they type on the keyboard is saved in the name variable. The last statement then greets the user.

What is your name?Edward
Hello, Edward

The above output gives an overview of what goes on. And remember I said, the input function optionally accepts the prompt text:

if __name__ == "__main__":

    print("What is your name?")

    name = input()

    print("Hello,", name)

In the above program, I used a separate print function for the prompt. Regardless, the output should be the same.

That was actually it. I hope it's been informative to you. Thanks for reading.