How Can I Read Inputs as Numbers in Python?

Are you ready to take your Python knowledge to the next level? Let’s have a little fun with the inputs. Only instead of having you enter your name, we’ll work with numbers.

You can play with this program in our interactive shell:

Exercise: Run the program—is the result what you expected?


It’s a nice, simple calculator. You enter in two numbers and it adds them together to give you an answer. Simple, right? Well, let’s see what happens when we enter the two numbers.

Unless math changed overnight, we’re pretty confident in saying that 3+3 does not equal 33.

What’s wrong?

Explanation

It’s that your output is a string, not an integer. That’s a problem. Your inputs are outputting the answers in a string format, not in an integer like they should be.

It’s not as embarrassing as you think. You’re learning and this information will be helpful to you as you progress in your Python efforts.

By default, inputs offer a string value. That means when you’re entering a number into your input, it’s not inputting 3, the number. It’s entering 3 as a string or “3” (yes that includes the quotation marks. If you created a variable, you could give it the number 3, or you could put it in quotations and make it a string. You’re doing the latter when you’re putting it in your inputs.

That’s how you end up with 3+3 being 33. You can’t properly add together strings. It would be like if you entered a name in the input and added it by three. You would get that name printed three times in a row (and with no spaces).

You can’t add strings together mathematically. And because of that, you can’t subtract, multiply, or divide them. You’ll get the same type error informing you that you have a string.

How do you check if your input is a string or integer in Python?

If you’re skeptical of this and want further proof that you’re working with strings. Create print() statements for both a and b. Add type to both of them. You can format it like this:

You’ll need to wrap both variables in the type() function. Type is the simplest way for you to determine if you have a string, integer, float, or a Boolean.

Both statements should give you a <class 'str'> answer, meaning that it’s a string.

Now, that’s not the only method you can use to determine the data type of the object. There’s another built-in function you can use called isinstance().

Unlike the type() function, isinstance() requires two parameters: the variable and the class. The function returns a True or False answer. For this example, you would wrap the variable inside the isinstance() function and you would include int as the second parameter.

And when you run the code, this should be your result in the CLI.

By comparison, if you change the second parameter to str, then it will return True as an answer for your inputs.

Whichever one you go with for your Python script is up to you. Both work great. If you’re new to Python, it might be better to go with the type() function.

Convert Your Input String to Integer

Don’t fret though. You’ve gotten this far in your Python process. You can overcome this roadblock. What you need to do is adjust your variable. You’ll need to convert your answers to integers. How do you do that?

It’s easy, to be honest. And there are a couple of different ways you can do this.

Changing a string to an integer in Python

One of the built-in functions available for you to use in Python is the integer function (or int()). It works like this:

If you have a string like x = "1", then all you need to do is update your variable and do x = int(x) so when you print(x) you’ll get 1. You might not notice it at first but try to do a print statement like print(x+5). Your output will equal six. Whereas if you tried it the other way with it as a string, you’d get an error.

Now there are two ways you can use the integer function for the previous script that we’ve tried. You can update the variables after the inputs. Here’s the example:

That converts both strings into numbers and you can add/subtract/multiply/divide them easily from there in your print statement.

When you run it, you should be getting the answers that your heart desires.

Breathe a sigh of relief. You’ve solved a problem.

Complete Solution

There is a shorter way for you to convert your string into a number. You can wrap your inputs inside the integer function.

Try it yourself in our interactive browser shell:

Exercise: Do you still get the unexpected behavior when calculating arithmetic operations on two numbers?

When you do it this way, you can avoid creating the extra variables. It keeps your code looking great.

If you’re still uncomfortable with using that many parentheses, then it might be simpler for you to use the first method.

You don’t need to use built-in functions. There is a Python package out there that can help.

Using pyinputplus for Your Inputs

If you’ve heard of or read “Automate The Boring Stuff With Python” then you probably know about Al Sweigart and some of the Python programs that he’s made. If you haven’t heard of either, then you need to check out his website. It’s perfect for Python beginners.

One of the Python packages that he’s created, which is available on GitHub, is known as pyinputplus. It’s a package that allows you to create inputs for your Python script.

With this program, you don’t need to worry about creating additional variables just to convert a string into an integer like we did above. With this package, you can create inputs for numbers and integers. And it works for both Python 2 and 3.

You’ll need to install it onto your command line. If you use PIP, then just do pip install pyinputplus. From there, you’ll need to import the package into your script like this.

And then there are two functions you can use to create integers. You can use the inputNum or the inputInt function. For this example, we used the latter. Either one of these will work. You can have your script formatted like this:

The best part about this package is you don’t even need to worry and check if it’s a string or integer. You have what you need and it saves you time.

What’s nice about pyinputplus is it allows you to get creative with your inputs. There’s so much you can do with it. Play around with it. You won’t be disappointed.

Whichever route you go, do know that there are solutions available to help you. What seems like a challenge and frustration now won’t be that way when you figure it out. I promise that Python programming efforts will be worth it.