Working With Strings and Numbers
When you get user input using the built-in
input() function, the result is always a string. There are many other situations in which input is given to a program as a string. Sometimes those strings contain numbers, and they need to be fed into calculations. In this lesson, you’ll learn how to deal with strings of numbers.
What if you use the
+ (plus operator) and say
num + num? What does that do? You might have thought about this already, that it’s going to concatenate them together. It doesn’t equal
"2", which is
22. Well, the string
22, not the number.
If you took
num and you used the
* (asterisk), which is the multiplication operator, and you said, okay, multiply that by
5. Well, that should equal
10, right? Well, in this case, it’s going to create a string with the number
"2" being repeated five times, so you get a string of five
2s, concatenating them all together.
When using strings with arithmetic operators, now you can tell that it behaves a little differently than you might have thought. The
+ concatenates two strings together, as you practiced before, and the
*, or the star multiplication operator, creates multiple copies of a string.
7 is just going to be the number of times that the number that’s inside of
num, the string, is concatenated together. You can actually do it with other things, right? You could say
5 * "Hello".
You can’t multiply these two together because it says that you can’t multiply a sequence by a non-int of type
str. So the sequence it’s talking about is the string
"12", which is a sequence of two characters, and then trying to multiply it by
"3", which is a non-integer string.
03:46 So what do you do with all these strings that you would prefer to be numbers? You’re going to use some built-in functions for that. The function converts objects into whole numbers, or also known as integers.
Let’s try these out. To test this out, head back to IDLE, and you’re going to create a new file. You can use the pull-down menu of File and New, or you can use the key command of Command + N or Control + N on Windows to open up an edit window over here. In the edit window, we’ll create a
num object, and it will accept the input from a user. And this is the prompt.
"Enter a number to be doubled: ". Close that out. So the built-in
input() function is going to prompt this, and then whatever the value that’s typed in will be assigned to the variable
num. Then to create
doubled_num, you will say
num is multiplied by
2 and then print the output. Great. Go ahead and save that.
I’ll call it
doubled.py. And again, just save it on my desktop. In this case, if you pressed F5 to run it over here, it’s asking for a number to be doubled, and I will say, oh,
So how can you work with this? Well, we were dealing with the number
8 earlier. What you can do, again instead of just working with the number
"8" by itself as a string is you can convert a string like
"8" into an integer, and you can see
int() will convert that into an actual number of the value of
It doesn’t have the quotation marks around it like a string literal.
int stands for integer and converts objects into whole numbers, whereas
float() is going to convert it into a number with a decimal point.
So if I were to use
"8", you’ll see
8.0. Okay, so what do we need to do? Well, what we can do is convert it here and say
int() for the number. This is one solution. Let’s try it out.
So maybe it might be safer depending on what we want to type in, a little more flexible, to use
float() instead. So I’m going to do that. And if I run it after saving. Now, if I were to, like I said, type
2.5, it’ll say
5.0. Or even if I were to run it again and type in
12.0 or even
12 by itself, it will come back as a floating point.
Let’s say we have a variable called the
num_pancakes, and currently it’s
10. You’re super hungry. And you say,
"I am going to eat " concatenate the
num_pancakes to another string with
" pancakes" in it. Automatically print that out and insert the number.
Well, you can only concatenate a string, not an integer to a string. Since the variable
num_pancakes is a number, Python can’t can concatenate it with the other string. You need to convert the
num_pancakes integer into a string.
So let’s say you had a
total_pancakes that was
10, and these are how many that you’ve eaten so far, which is
5. So we’ve assigned two variables,
total_pancakes to the integer
pancakes_eaten to the integer
5, and you want to create a string that says
"Only " concatenate, and this time you’re converting to a string, and you’re going to say the
total_pancakes minus the pancakes that have been eaten, and concatenate that to show the number of pancakes that are left.
Create a string containing an integer, then convert that string into an actual integer using the built-in
int() function. Test that your new object is a number by multiplying it by another number and displaying the result.
Try out the previous exercise again, but use a floating-point number and the built-in
float() function. Try creating a string object and an integer object, then display them side by side with a single print using the
str() function. Okay. In this one, you’re going to use
input() twice to get two different numbers from a user and then multiply the numbers together and display the result.
Become a Member to join the conversation.