Skip to main content

Command Palette

Search for a command to run...

User input in Python

Published
2 min read
User input in Python
C

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.

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);

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)

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

More from this blog

D

Daily Dev Tips

887 posts

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊