• Save
  • Download
  • Clear Output
  • Runtime
  • Run All Cells
  • Difficulty Rating

Loading Runtime

There's a couple of things that I still think could be improved with our personalized_bmi function. The issues I've noticed will probably seem small, but I think will lead to some valuable demonstrations.

31.14186851211073
Sally 's Body-Mass Index is: 24.221453287197235

The first thing I want to point out is the extra space in the word Sally 's. This space is there because when we use the print function to print out multiple pieces of information, the print function will automatically put a space between each value –whether we want it there or not.

What we're really trying to do with this function is to build a single coherent piece of textual output that is informed by the values of the passed-in arguments. And there's a better tool for that than what we're currently doing. That tool is called f-strings.

F-strings are a way of inserting values into a string to create a single, coherent, larger piece of output. Let's look at an example.

Say we have a variable called age and we've stored the value of 31 to it.

We want to take this age and insert it into the following string:

"I am __ years old." So that the number 31 takes the place of the blank portion.

We can do this by writing the string that we want to customize and then putting a lowercase letter f in front of the opening quotation marks, and then we put a set of opening and closing curly braces wherever we want to insert a value into the string. And we will put the name of the variable whose value we want to insert into the string inside of the curly braces.

I am 31 years old.

This practice is called "string formatting" –which is where the "f" in "f-string" comes from. There are lots of ways to format strings in Python, and f-strings are the best and easiest way to do it. Trust me.

Now let's use an f-string in our personalized_bmi function. Let's start out by writing out the whole string as we want it to show up. Put an f at the beginning, and then replace the parts that we want to be represented by variables by the correct curly-brace enclosed variable names.

Sally's BMI is 24.221453287197235.

Nice! Now our function is printing out single formatted string rather than different values separated by spaces.

However, I actually don't like that the only thing that this function is doing is printing. Let's return this completed string instead of printing it, and when we call the function the returned value will be automatically outputted for us anyway.

Sally's BMI is 24.221453287197235.

Last but not least, I want to round the output from the calculate_bmi function to two decimal places so that the returned string is a little bit more readable.

We can do this either by rounding the output of the calculate_bmi before it's returned or with some tricky formatting inside of the f-string. I'll show you both methods.

To easily round any number to two digits, we can put it inside of the round() function as the first argument, and pass a second argument to the round()` function that says how many digits we want to round it to, and voila!

24.22

We can also cause the rounding to happen as the value's getting inserted into the f-string by putting :.2f after the value that we want to be rounded. The 2 in this piece of syntax tells the f-string to round the number to two decimal places. However, this is not very readable syntax, so it's up to you which method you prefer.

Sally's BMI is 24.22.
Sally's BMI is 24.22.