CODE: TU16 POINTS: 25
Including variables in your text
The easiest way for iGCSE students to include is to use speech marks and a comma. This is simple and effective. However it always includes a space and can't be used for filenames and other more complex string handling.
Here is an example:
name = "Eric"
age = 74
print("Hello",name,"You are",age)
Now let's have a look with an f string:
print(f"Hello, {name}. You are {age}.")
The first big advantage is that you don't need to have spaces. So you can have strings with RM129.
Exercise 1: Create a float variable for Ringgit and print it as "RM129"
Here is an example:
name = "Eric"
age = 74
print("Hello",name,"You are",age)
Now let's have a look with an f string:
print(f"Hello, {name}. You are {age}.")
The first big advantage is that you don't need to have spaces. So you can have strings with RM129.
Exercise 1: Create a float variable for Ringgit and print it as "RM129"
Creating tables
This demonstration code shows you how to easily create tables with lists of numbers.
Left justify uses <15
Right justify uses >15
centred uses ^15
15 being the number of characters. Please note that if your data contains more characters then it will spill over.
To shorten numbers you can limit them to 2 floating places using .2f.
Exercise 2: Create a table of numbers with headings
number = 17
print(f"Binary: {number:b}\nHexadecimal: {number:x}\nDecimal: {number:d}")
Exercise 3: Create a quick binary convertor
Left justify uses <15
Right justify uses >15
centred uses ^15
15 being the number of characters. Please note that if your data contains more characters then it will spill over.
To shorten numbers you can limit them to 2 floating places using .2f.
Exercise 2: Create a table of numbers with headings
number = 17
print(f"Binary: {number:b}\nHexadecimal: {number:x}\nDecimal: {number:d}")
Exercise 3: Create a quick binary convertor
If you want more help, look at: Python f-string tips & cheat sheets