DEV Community

Lisa Ghosn
Lisa Ghosn

Posted on

F-Strings

I'm keen to see how f-strings work with the programme I wrote in my previous post. I was advised that they're much easier to work with and less prone to error.


so...

I have a lot of boxes of eggs in my fridge and I want to calculate how many omelettes I can make. each box of eggs contains six eggs and I need four eggs for each omelette.


Pycharm image of code to run the above task: number_of_eggs_needed_to_make_an_omlette = 4<br>
number_of_eggs_in_a_carton = 6<br>
number_of_egg_cartons = 9<br>
total_eggs = number_of_egg_cartons * number_of_eggs_in_a_carton<br>
omlettes = total_eggs // number_of_eggs_needed_to_make_an_omlette<br>
left_over_eggs = total_eggs % number_of_eggs_needed_to_make_an_omlette<br>
message1 = f'there are {total_eggs} eggs in total, this means you can make {omlettes} omlettes with {left_over_eggs} eggs left over'<br>
print(message1)

'to make 25 omlettes, you will need 100 eggs. That means you need to buy 16 cartons of eggs'

'there are 54 eggs in total, this means you can make 13 omlettes with 2 eggs left over'


It's a lot easier to figure out which placeholder represents which variable, so the code is a lot easier to read. I'm also happy to see the code shrink a bit!


Top comments (0)