DEV Community

Cover image for User input in Python
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

User input in Python

Accepting user input in applications is pretty standard as we would like to have the user give us some information.

In Python we can achieve this quite easily. Today, our end goal is to make a script that asks for some details and then prints these back to the user.

User input in Python

Using variables in Python

First of all, we must understand how we can use variables in Python, and this process is pretty simple.

animal = "cat"
print("Your favorite animal is a " + animal);
Enter fullscreen mode Exit fullscreen mode

And this will print the following line:

Your favorite animal is a cat

Of course, I'm just guessing your favorite animal is a cat here and could be entirely off.
So let's fix that and allow the user to state the correct answer.

Accepting inputs in Python

We can change the variable we had to input(), allowing us to accept user input.

print("What is your favorite animal?")
animal = input()
print("Your favorite animal is a " + animal)
Enter fullscreen mode Exit fullscreen mode

Now when we run the script, the user will be prompted to give us input.

User input in Python

And it's really as simple as using that input() command to capture a variable!

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Latest comments (3)

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

Glad to see you diving deeper into python. Outside of toy examples I cannot think of an application I have ever had for input typically I accept user input in the form of stdin, or a file. You can use parsers from the built in argparse module, or the more sophisticated click library. For super basic things, I often reach for sys.argv, its not fancy but works.

Here is a super basic example.

import sys

print("Your favorite animal is a " + sys.argv[1])
Enter fullscreen mode Exit fullscreen mode
$ python example.py penguin
Your favorite animal is a penguin
Enter fullscreen mode Exit fullscreen mode

You can get super quick and dirty by adding a #! python at the top of example.py so that you can leave off the python part of the command. Fair warning, doing this can lead to some good "works on my machine" errors if you happen to try to share a script with this in it.

#! python
import sys

print("Your favorite animal is a " + sys.argv[1])
Enter fullscreen mode Exit fullscreen mode
$ chmod +x example.py
$ ./example.py penguin
Your favorite animal is a penguin
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Wow thanks!
Yeah I was thinking how often you might need it, perhaps if you would make some kind of console command, but not sure why you would use Python for that.

Think i'm also going to look at the visual interface so far Tkinter looks the best, what's you're take on that?

Collapse
 
waylonwalker profile image
Waylon Walker

I've never done a gui interface with python. I stick to using python for backend/data things and the web for front end UI. Nothing wrong with doing UI in python, I've just not had a use case for it.