top of page
top

Python 1d - Using Variables

noun-code-2821463-FFFFFF.png

Printing Variables Within Sentences

Join sentences and variables together using a plus symbol (+). Joining strings together like this is called concatenation.

name = "Marina"

print("Hello " + name + ", nice to meet you.")

=

Hello Marina, nice to meet you.

Remember to use speech marks for your printed statements but no speech marks for variable names.

​

You need to use the + symbol before and after each variable.

direction = "north"

country = "Wales"

print("Have you been to the " + direction + " of " + country + "?")

=

Have you been to the north of Wales?

Commas can be used an alternative to the + symbol but they will automatically add a space.

day = "Saturday"

print("My birthday is on a" + day + "this year.")

print("My birthday is on a" , day , "this year.")

=

My birthday is on aSaturdaythis year.

My birthday is on a Saturday this year.

Using Variables Task 1 (Pizza Toppings)

Use a variable named topping1 and another named topping2.

​

Print a sentence that uses both variables names.

Example solution:

My favourite pizza is ham and mushroom.

Printing Number Variables Within Sentences

To join strings and number values then you must use a comma as a plus will not work:

cookies = 4

print("Munch! There's only" , cookies , "left.")

=

Munch! There's only 4 cookies left.

You need to use a comma before and after each variable.

Using Variables Task 2 (Stars)

Make a variable named stars and set it to a large number.

Print a sentence with the stars variable.

Example solution:

I think there are 827392012 stars in the sky!

Using Variables Task 3 (Age & Month)

Use a variable named age and set it to your current age.

Make a variable named month and set it to the month you were born.

​

Remember to use speech marks for text, e.g. month = "August" but no speech marks for numbers (your age).

​

Print a sentence that uses both variables names.

Example solution:

I am 14 and I was born in August.

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.

name = "Tony Stark"
alias = "Iron Man"
print(f"Did you know {name} is actually {alias}?")

=

Did you know Tony Stark is actually Iron Man?

Using Variables Task 4 (F-Strings)

Create and give a value to three variables:

  • movie_name

  • actor

  • year

​

Use an f-string to print a sentence that uses all three variables.

Example solution:

Did you know that Harry Potter and the Order of the Phoenix stars Daniel Radcliffe and was released in 2007?

bottom of page