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.