Python 1d - Using Variables
Printing Variables Within Sentences
Join sentences and variables together using a plus symbol (+). Joining sentences together like this is called concatenation.

=

Remember to use speech marks for your printed statements but print the variable name to display the variable value.

=

You need to use the + symbol before and after each variable.
Practice Task 1
1. On line 1 write a variable with the name sandwich and the value of a sandwich filling.
2. On line 2 print a sentence that uses the variable name sandwich.
3. On line 3 change the variable sandwich to equal a different filling.
4. On line 4 print the same sentence as line 2.
Example solution:

Printing Number Variables Within Sentences
To join strings and number values then you must use a comma as a plus will not work:

=

You need to use a comma before and after each variable.
Practice Task 2
1. Create a variable called bottles and give it the value 99.
2. print a sentence that uses the variable name.
3. Change the variable value to 98.
4. print the same sentence again.

Example solution:
Using f-Strings
Another method of using variables within a printed sentence is to use f-strings.
Type the letter f before your output and place your variable names in curly brackets - { }
Variables of any data type can be used with f-strings.


Practice Task 3
1. Create three variables:
-
movie_name
-
actor
-
year
2. Use an f-string to print a sentence that uses all three variables.
Example solution:

Embedded Python Editor
Powered by trinket.io