Instruction
Let's start by showing how to conveniently build strings that contain multiple variables. Consider the following code:
user_age = 25 user_name = 'Kate' user_location = 'Brazil' greeting = 'Hello, ' + user_name + '! You are ' + str(user_age) + '. Welcome to ' + user_location + '!' print(greeting)
The last line isn't very readable. We need to use a lot of plus signs and convert integers to strings; it's hard to imagine what the final string will look like. Luckily, this code can be simplified:
greeting = 'Hello, {}! You are {}. Welcome to {}!'.format(user_name, user_age, user_location)
Easier to read, isn't it? Inside the string, we put three placeholders, denoted by {}. Then we used the .format() function and provided three values (the user_name, user_age and user_location variables) that we wanted to be placed in those placeholders. Values are inserted from left to right into the placeholders. Note that we didn't need to convert user_age to a string!
Exercise
Ask the user how much they earned last month:
How much did you earn last month?
Then, ask how much they spent:
How much did you spend?
Finally, show the following sentence:
You earned {x} and spent {y}. The difference is {x-y}.
Notice: you need to cast user input values to integers before subtracting the amount spent from the amount earned.



