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

Unlock This Lesson

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

Unlock This Lesson

Work With Strings and Numbers (Solution)

00:00 I’ve opened up a new file and wrote down a couple of notes for the instructions. I want to get two numbers from the user, multiply them, and then display the result.

00:08 That’s the things that I want to do in the multiplication. There’s actually, like, another step in there. I’m also going to want to convert them to numbers,

00:21 so I can do that at some point, but I need to do it before I multiply them. And that’s because you can’t actually multiply two strings together. That’s just going to be an error.

00:30 So I hop over to the REPL just to show this to you. If I have two strings and try to multiply them together,

00:39 I’m going to get a TypeError that Python can’t multiply sequences by non-int of type str. So a nicely descriptive message that just tells me that that doesn’t work.

00:48 But if you do collect user input, it’s always going to be a string. Even if the user types in a number, try that out too. So I will assign the return value of input() to a.

01:00 I’m just going to give an empty prompt here.

01:04 And so the prompt, it prompts me now to input something. If I input here 4, so thinking that I’m inputting a number, what I get as a result is still a string.

01:15 As you can see, it’s of class str. It looks like that. So if you’re collecting user input, it’s always going to arrive as a string. And then if you’re trying to multiply two strings together, you’re going to run into an error.

01:28 There’s a little fun thing on the side though. If you had a different program that had a fixed integer that you’re multiplying the user input with, let’s say, then if you don’t do a type conversion here, you could run into an output that you might not expect.

01:44 We have a, which points to 4, right? And let’s say the program multiplies the input of the user by 20, right? So what you get in that case is a string concatenation.

01:57 Python concatenates the input string twenty times. So you get twenty fours as an output, which would be a strange calculation result for doing 4 times 20.

02:08 So to avoid that, you need to explicitly convert it. So you could, for example, convert a to an integer and then do the multiplication.

02:18 And that works out, right? That works correctly, and math makes sense again. So that’s just to keep in mind that you will need to convert these numbers. With that, let’s get started by collecting the two numbers from the user.

02:33 a is input("Enter a number: "), give them a prompt to make it a bit more readable, and then the same thing again for another number.

02:43 I’ll say b is input("Enter another number: ").

02:48 Then I want to convert them to numbers so that I can multiply them. I’ll do it in one step. So I’ll convert to floats because in the output I want a float,

03:01 so saying explicitly converting both a and b to floating-point numbers, and then assigning that to a variable called product.

03:11 Here I’m doing the conversion and the multiplication in one step. And then finally, I need to display the result.

03:19 I’m going to do this just using string concatenation, which, as you’ll see, is going to get pretty long. You’ve probably already encountered a better way of doing that.

03:28 But for now, let’s just stick with string concatenation because I also want to show you something similar to the multiplication that doesn’t work with two strings.

03:36 You can’t do concatenation if not both of them are strings. So in order to be able to display this message, "The product of", here you can just use the variable that I’ve collected because a is a string, right?

03:54 I’m only converting it in the multiplication to a floating-point number, so a still points to the original string entered. So I can say the product of a and

04:06 b is,

04:12 and now if I just add the product here without any conversion, I’d again run into an error. Let’s try that out in the console. So here what I’m trying to do is concatenate a string and a floating-point number.

04:27 So it’s marked it over here. I’m going to say "a", this is my string, plus a floating-point number, 0.2. And you can see that Python again gives a TypeError where it tells you that it can only concatenate strings to strings and not a float to a string.

04:44 So in order to be able to put product into this string that you’re concatenating here, you’ll have to explicitly convert it to a string by using str() and passing product as the argument.

04:55 With that, it’ll work out, and you can continue also adding yet another string, which in this case, it’s just a period.

05:03 All right, so this is a really, really long way of assembling this message, basically just using string concatenation, but let’s give it a try and see if it works.

05:12 I save and run it. Enter number 2, enter another number 4, and The product of 2 and 4 is 8.0. So I am getting the expected output.

05:23 The result of the input looks like a floating-point number, but here in the print() call, you’re converting it to a string before concatenating it with the other pieces of the string that you’re assembling.

05:37 If you’ve used .format() or an f-string for this final message, that’s also a great way to go. And in the next lesson, you’ll actually practice using these a bit more.

05:47 So if you’re stuck with string concatenation up to now, you’ll get a chance to try out something new.

Become a Member to join the conversation.