MA1008 Week 6 (Strings)
MA1008 Week 6 (Strings)
MA1008
Tutorial Strings
Week 6
Question 1
Human 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
Computer 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
Text I n t r o d u c t i o n t o C o m p u t a t i o n a l T h i n k i n g
pet = "cat"
age = 10
print(f"My {pet} is {age} years old.")
The alternative is to type out print("My", pet, "is", age, "years old.").
That is a little more cumbersome.
The f-string’s benefit is more obvious when formatting the printing of
numbers.
import math
print(math.pi)
print(f"The value of pi to 2 decimal places is: {math.pi:.2f}")
Question 3
Write down the output of the following statements. Mark the spaces with a .
for fahr in range (32, 200, 50):
cels = (fahr-32)*5/9
print(f"{fahr:>4d} Fahrenheit = {cels:<6.2f} Celsius")
Print as float
Right justify
2 decimal places
4 spaces
6 spaces
Print as integer
Left justify
32 Fahrenheit=0.00Celsius
82 Fahrenheit = 27.78Celsius
132 Fahrenheit = 55.56Celsius
182 Fahrenheit = 83.33Celsius
Take note that parameters like :>4d and :<6.2f are optional. They do (usually) cause syntax errors when not
included. You can try the line print(f"{fahr} Fahrenheit = {cels} Celsius") to see the difference.
Question 4
4. Given the assignment
S = "I am testing my program"
What is the outcome of the following statements?
Human 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Computer 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Text I a m t e s t i n g m y p r o g r a m
i. S[0] = "J" Error. More specifically, type error because string is not mutable.
ii. print(S)
I am testing my program
iii. print(S[::3])
Imei oa ( represents the space character)
iv. print(S[12:4:-1])
gnitset
Question 5
5. What are printed by the following expressions:
i. print("Nanyang"*3)
NanyangNanyangNanyang
ii. print("Nanyang"*3.0)
Error. Can’t multiply string by non-integer types.
iii. print("Nanyang"*-3)
An empty string.Same result if “Nanyang”*0.
iv. print("Nanyang" + "Technological" + "University")
NanyangTechnologicalUniversity
v. print("Nanyang" - "N")
Error. “-”is undefined for strings.
Question 6
6. Write down what is printed by the
code below once you give the input (use
your own input).
To clarify, the question is asking how we can for example store the number 5
in the string “12345”, as an integer.
lastDigit = int(str(12345)[-1])
Programming Strings
Question 1
1. Write a program that would prompt for a string and then print the
ASCII value of each character in the string.
for c in in_str: #for loop that has as many iterations as the number of chars in in_str. c is the char at that iteration
if c.isalpha():
new_c = ord(c) + 5
if c.isupper() and new_c > ord("Z"): # upper case letter that needs to wraparound
new_c = new_c - ord("Z") + ord("A") – 1 #formula to wraparound
elif c.islower() and new_c > ord("z"): #lowercase letter that needs to wraparound
new_c = new_c - ord("z") + ord("a") – 1 #formula to wraparound
elif c.isdigit(): # it's a digit
new_c = ord(c) + 3
if new_c > ord("9"): #digit that needs to wraparound
new_c = new_c - ord("9") + ord("0") – 1 #formula to wraparound
else:
new_c = ord(c) #no change
out_str = out_str + chr(new_c) #chr() is the function to convert Ascii value back to character.
import math
area = math.pi*radius*radius
circ = math.pi*radius*2