Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 2: Guess that number game
Lecture: String formatting

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we have our app working but the user feedback could be a little better let's go down here and try to improve this message we sent to the user
0:09 instead of just too low, let's say something like your guess of... and we can put in this number this guess they had here
0:17 we saw that we could do this by combining strings by saying guess so maybe you'll think well this is a way I can do this here
0:23 and this will just combine it that does work in certain languages say your guess of let's say 70 was too low, something like that.
0:31 Now, PyCharm is kind of giving away the surprise here, but this is not really love that says
0:39 the string int conversion with plus is not going to work out so well for you So let's just run it and find out.
0:45 So if I enter something that is going to be for sure too low like 0, let's see what happens, you get this exception,
0:50 you cannot convert int object to string, implicitly. Well we technically could explicitly convert it
0:55 but it turns out that there is a much better way in Python. What we want to do is we kind of want to put something into this space,
1:02 now the way we can do this nice way is we can use these little curly braces inside the string and then call a function on the string called format
1:11 and pass whatever we want to go in there. And so, into this location we can say we'd like to take the guess and explicitly convert it to a string,
1:19 whatever that means, this time it's a number, so that is pretty straightforward, and then we are going to put the result of it right there.
1:26 We can actually pass many parameters here, we could have something like name, like let's change this really quick name=input player what is your name
1:36 and these little numbers that go in here talk about the order in which there is specified in the format method.
1:42 So guess is first, everything is zero based, so it gets a zero, name is second, so it's {1}, so we could say sorry {1} your guess of {0} was too low.
1:54 Now let's go and run this, see if everything is working, enter your name, my name is Michael, enter a guess, I am going to put 0,
2:01 sorry Michael your guess of 0 was too low. Now, we are just scratching the surface of the power of this format method.
2:09 We can if we wanted to switch the order here, if we specify the place holders in the string in the same order as they are specified as arguments there
2:19 we can actually omit the number now this will say sorry name your guess of... whatever the number was, was too low.
2:27 We can put in format specifiers here to say I'd like to separate this with commas or convert it to currency, or all sorts of sort of format specifiers
2:37 we can take dictionaries and name these things and pull items out of the dictionary by name and then put their values in here, it's very rich,
2:44 but let's just go with this for now. So we'll do the too high case here, so your guess was too high, and finally,
2:50 we'll just say something nice about winning excellent work so and so, you won it was something like this, right?
3:00 so excellent work, whatever the name is and you won it was whatever the guess was. So, let's play the game again,
3:06 all right, what's your name, now I've got to enter my name, it's Michael, I am going to guess 50- wow 50 was too high, 25- too low, 35, 40, 45 47,
3:18 what do you think, 46, chances are likely, YES! excellent work Michael, you won, it was 46.
3:25 See how we can use string format to really simplify and clarify how we convert values into strings.

Talk Python's Mastodon Michael Kennedy's Mastodon