Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Working With Strings and Numbers

00:00 Now that you’ve worked with user input a bit, let’s talk about working with strings and numbers.

00:07 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.

00:27 And you’ll also see how arithmetic operations work on strings and how they often lead to surprising results. Then you’ll learn how to convert between strings and number types.

00:39 Let’s start this out in the interactive window.

00:44 Try this out. I’ll have you create a variable called num and then assign it the string with the number "2" in it. So this is num, a string literal with "2" in it.

00:55 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 4, but "2" and "2", which is 22. Well, the string 22, not the number.

01:13 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.

01:40 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.

02:02 And it doesn’t matter if the number is on the right side of the expression or the left. Say you had 7 * num. It still sees num as a string.

02:14 And 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".

02:28 One thing that you can’t do is let’s say you said the string of "12" multiplied by the string of "3". That will give you a TypeError.

02:39 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:02 So this doesn’t work. It’s raising a TypeError. And similarly, what if you took the string of "3" and you tried to add it to the number 3 by itself? That gives another type of TypeError.

03:17 It says you can only concatenate str, not an int, to a str. If an object on either side of the + is a string, then Python tries to perform string concatenation.

03:30 It will only perform addition if both objects are numbers. You would have to first convert the string "3" into a number before you could add it to 3. Let’s look at how to do that.

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.

03:58 Sometimes you’re going to want objects to be turned into numbers that actually have decimal points. In that case, you’ll want to use the built in float() function.

04:08 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.

04:39 You’re saying "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.

05:02 I’ll call it doubled or 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, 8, please.

05:17 Oh, you see my error? I created a variable with the wrong name here. I didn’t use the correct name again, so I need to either change it here or change it there.

05:26 So I’m going to change here and save. So, a little NameError issue. All right, now that I’ve saved it, and we can run it again. Okay. This time now I can type 8.

05:35 Instead of getting 8 times 2 of 16, I get 88, so8 and 8, because it’s doing that for two strings. So we’ve got to figure out how to change this.

05:48 Open up a new shell window here to kind of clean things up a little bit. You can keep going in yours. I just want to not have so much typing on the bottom.

05:56 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 8 here.

06:21 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.

06:33 So if I were to use float() for "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.

06:52 So I just saved it again. If I press F5 to run it … oh, this time let’s say "12". Great. There, 12 multiplied by 2 is 24.

07:02 But what if I wanted to type in, I don’t know, 2.5? Well, then we would need it to be a float, and it wouldn’t be bad if this was 12.0.

07:15 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.

07:39 Why did we use float() instead of int()? If you give the built-in int() function something that is not in base 10, like if you were to say "2.5", it gets a little angry.

07:52 It says that’s an invalid literal for int() with this requirement of a base 10. So even if you said "12.0", you get the same kind of error.

08:04 So float() gives you a lot more flexibility depending on the string that you’re putting into it.

08:11 Sometimes you need to convert a number into a string. To do that reverse, there is a function for it too. Let me give you an example. Let’s say we’re going to do some concatenation again.

08:22 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.

08:45 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.

09:02 And that’s where we use the built-in str() function.

09:09 Just like int() and float(), there’s a built-in function, str(), that returns a string version of an object.

09:20 Let me copy this to save a little effort. And here you could add the built-in str() function, which takes an object and it outputs a string. Great.

09:32 Let’s see if that works. Yep. That works great. What’s kind of cool is the built-in str() function can actually handle arithmetic expressions right inside of it.

09:43 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 10 and 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.

10:23 So it can do that subtraction right inside there. Let’s look at some additional exercises for you to practice with these.

10:33 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.

10:49 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.

11:16 The printout should look something like this.

11:21 Up next, you’re going to learn how to streamline your print() function.

Avatar image for s150042028

s150042028 on Sept. 16, 2023

animals = "Animals"
badger = "Badber"
honey = "Honey Bee"
honey2 = "Honey Badger"
string1 = " Filet Mignon"
string2 = "Brisket "
string3 = " Cheeseburger "
string11 = "Becomes"
string12 = "becomes"
string13 = "BESR"
string14 = " bEautiful"
my_input = input("Please Wake up :")
test= "11"
new_test = int(test)
new_test = new_test * 5
new2_test = float(test)
new2_test = new2_test * 5
test_string = "3"
test_int = 3
intput1= input()
intput2= input()
mut=int(intput1) * int(intput2)
print("the product of", int(intput1), "and", int(intput2), "is", float(mut))

Become a Member to join the conversation.