User input in Python


How to accept user input in a Python script

20 May, 2021 · 2 min read

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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Speedtest your connection in Python

21 Jun, 2021 · 2 min read

Speedtest your connection in Python

F-strings in Python

7 Jun, 2021 · 2 min read

F-strings in Python

Join 2099 devs and subscribe to my newsletter