Using the Input Function To Get User Input in Python

This will be a short write up on how you can get user input in Python. It’s essential to to be able to get user input, even in the most basic of applications. You might want to know the users name or age. Or perhaps you want to make a calculation based on the users input. Thankfully, the basics of getting user input in Python are very simple, though there are a few complications that I will discuss shortly!


Using the input() function

Getting user input in Python is done by using the built-inĀ input() function. Say you want to ask the user for his/her name. You create a variable called “name”, assign the input function to it and thus store the input in that variable. This is what I’ve done in the following example:

# create a variable and assign the input function to it.
name = input("Please enter your name")

# print out the given name
print("Your name is " + name + "!")

You can also use numbers and arithmetic operators and make some basic calculations. What you need to look out for, however, is that Python can’t combine a String and an integer. Thus, trying to print this to the console will result in an error. A common workaround for this is casting the input variable to an integer. You will also need to use a formatted String to print the integer to the console. This example should give you an idea of how it works:

# ask for first number
first_number = input("Enter the first number: ")
# cast the first String to an int
first_number = int(first_number)
# ask for second number
second_number = input("Enter the second number: ")
# cast the second String to an int
second_number = int(second_number)
# do any type of calculation
calculation = first_number * second_number
# print result
print(f"The result is {calculation}")

Demonstration of user input and calculation in Python

This will work perfectly fine if the user inputs a number. However, it will cause issues if the given input is empty, or any other character besides a number. Although this can be resolved, it involves some more advanced topics, so I won’t be covering it here. Don’t forget that you can always check Python’s documentation if you want to figure it out for yourself, though!


Wrapping up

As you can see, getting input from a user in Python is quite straightforward. Although there are a few complications, part of being a programmer is figuring out how to solve them. I will be looking into posting an article about error handling and conditional logic in the future, but for now, this will have to do!

Let me know if this was helpful or if you would like to see more of these types or articles in the future. As always, if you have questions or concerns, feel free to comment below. Lastly, make sure to share the article if you liked it!

CSS: Introduction to the Stylesheet Explaining Comparison Operators in Python Control Your Code: Conditional Logic in Python
View Comments
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.