# How Can I Read Inputs as Numbers in Python?

Rate this post

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.

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.