December 1, 2022


Hi, in this post we are going to briefly learn how to use variables in strings.


numberOfEggsPerHead  = 2

print("Please type in the number of people : ")
userNumberOfHeads = int(input())
numberOfEggs = 2 * userNumberOfHeads

print("Since each person gets {} eggs then you have to buy {} eggs for {} people"
.format(numberOfEggsPerHead, numberOfEggs,userNumberOfHeads ))




You hang the “.format” method directly on the string while using the “{}” symbol to denote the spot that the corresponding variable will be inserted. Afterwards, add the variables as arguments to the format call in the desired order.


numberOfEggsPerHead  is a variable of type “integer” which stores the number of eggs each person is able to get.  In this case, each person gets only 2 eggs. You can adjust the number by changing the value after the “=” symbol.

userNumberOfHeads is another variable of type “integer” but unlike the variable above, this stores the value that the user typed in. Due to the user input being of type “string”, for the subsequent calculations, the value has to be converted into a number type. In this case the value is changed to a in type using the int function.

numberOfEggs, this stores the number of eggs required based on the number of persons that was provided by the user.

Pseudo Code

  • Ask the user for the number of persons
  • Calculate the number of eggs needed
  • Show the user the calculation result

Code run result

Please type in the number of people : 
Since each person gets 2 eggs then you have to buy 46 eggs for 23 people


No disclaimer here 🙂.


Input and Output

Read input as a float in Python