ENGG1330
Computer Programming I
| What It Represents | Text Stored in a String | What We Can Do |
|---|---|---|
| Webpage (HTML) | <h1>Hello, Ada!</h1> | Build a page heading |
| Python program | print('Hello, Ada!') | Generate source code |
| App response (JSON) | {"temperature": 24.5} | Read weather data |
| DNA sequence | ACGTGCAATG | Find a pattern |
Strings carry structure, instructions, and data
text = 'Python'print(text)print(len(text))print(text[0])print(len(''))text = 'café'print(text)print(len(text))print(text[-1])print('🍌')print("Hello 'Python'")print('Hello "Python"')print('Hello \'Python\'')print('Hello 'Python'')| Escape | Meaning |
|---|---|
\n | New line |
\t | Next tab stop |
\' | Single quote |
\" | Double quote |
\\ | Backslash |
text = 'Hello\nWorld'print(text)print(repr(text))repr(text) returns a string showing quotes and escape sequencestext = '*\t*\n**\t**\n***\t***'print(text)text = """Thisisamultiline string"""print(text)print(repr(text))print('C:\\some\name')print(r'C:\some\name')ENGG1330
Computer Programming I
description = 'immutable'message = 'Strings are ' + description + ' at all times'print(message)message = ('Vigorous writing is concise. ' 'Every word should contribute.')print(message)first = 'Py'second = 'thon'print(first + second)print('Ha' * 3)print(3 * 'un' + 'ium')print(repr('Ha' * 0))print(repr('Ha' * -2))text = "You miss all shots you don't take"for index in [0, -1, -10, 10]: print(index, repr(text[index]))text[0]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text[-1]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text[-10]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text[10]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text = 'dirk'for index in range(len(text)): if text[index] == 'i' or text[index] == 'u': print('Found i or u') breakfor char in text: if char == 'i' or char == 'u': print('Found i or u') breakword = input('Word: ')times = int(input('Enthusiasm (1-10): '))for char in word: print('Give me a ' + char + '! ' + char)print('What does that spell?')print((word + '!!! ') * times)print('a' in 'program')print('at' not in 'battle')print('battle' in 'at')word = input('Uppercase word: ')an_letters = 'AEFHILMNORSX'for char in word: article = 'a' if char in an_letters: article = 'an' print('Give me ' + article + ' ' + char + '! ' + char)print(word, '!!!')text = "You miss all shots you don't take"print(text[13:17])print(text[4:])print(text[:3])text[13:17]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[4:]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[:3]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text = "You miss all shots you don't take"print(text[-4:])print(text[-10:-5])print(text[-10:])text[-4:]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text[-10:-5]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text[-10:]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e | ||||||
| -33 | -32 | -31 | -30 | -29 | -28 | -27 | -26 | -25 | -24 | -23 | -22 | -21 | -20 | -19 | -18 | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
text = "You miss all shots you don't take"print(text[1:10:1], text[1:10:2], sep=" | ")print(text[1:10:3])print(text[::3])text[1:10:1]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[1:10:2]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[1:10:3]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[::3]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text = "You miss all shots you don't take"print(text[10:1:-1])print(text[10:1:-2])print(text[::-1])text[10:1:-1]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[10:1:-2]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text[::-1]
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text = 'Python'print(text[:100])print(repr(text[100:]))text = 'Python'print(text[100])print('Apple' == 'Apple')print('Apple' != 'Apple')print('Apple' < 'Apple')print('Apple' <= 'Apple')print('cat' < 'car')print('HKU' < 'CUHK')ENGG1330
Computer Programming I
| Question | Reference |
|---|---|
| A function such as len or ord | Built-in Functions |
| An operation such as upper or split | String Methods |
def is_palindrome(text): return text == text[::-1]print(is_palindrome('test'))print(is_palindrome('madam'))print(is_palindrome('Anna'))'madam'Parameter: textReturn value: True'Anna'Case-sensitive comparison'annA'def double(number): return number * 2def show_double(number): print(number * 2)print(double(5) + 1)result = show_double(5)print(result)def is_palindrome(text): index = 0 for char in reversed(text): if char != text[index]: return False index += 1 return Trueprint(is_palindrome('madam'))print(is_palindrome('Anna'))print(ord('a'), ord('A'))print(ord('1'), ord('2'))print(ord('é'))print(chr(65))print(chr(ord('é')))for number in range(65, 91): print(chr(number), end=' ')print()print('HKU' == "HKU")print('HKU' == 'HkU')print(ord('K'), ord('k'))print('HKU' < 'HkU')| Method | Purpose |
|---|---|
capitalize() | Capitalise the first character; lowercase the rest |
upper(), lower() | Change case |
split() | Split into a list of strings |
find() | Find a substring, or return −1 |
count() | Count non-overlapping occurrences |
isalnum(), isalpha() | Test alphanumeric or alphabetic text |
isdecimal() | Test for decimal digit characters |
strip() | Remove leading and trailing whitespace |
replace() | Return text with replacements |
name = 'DiRk'print(name.capitalize())print(name.upper())print(name.lower())print(name)text = 'red blue red'print(text.split())print(text.find('blue'))print(text.find('green'))print(text.count('red'))filename = 'report.csv'print(filename.endswith('.csv'))address = 'https://hku.hk'print(address.startswith('https://'))print('report.csv.bak'.endswith('.csv'))print('HKU1330'.isalnum())print('HKU'.isalpha())print('1330'.isdecimal())print('-3'.isdecimal())print('3.5'.isdecimal())print(''.isalpha())text = ' Hello HKU 'print(repr(text.strip()))print(repr(text.replace('HKU', 'Python')))print(repr(text))ENGG1330
Computer Programming I
text = 'change me'text[0] = 'a'text = 'delete me'del text[1]text = "You miss all shots you don't take"changed = text[:29] + text[30:]print(changed)print(text)Index 29: t
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text = "You miss all shots you don't take"changed = text[:29] + 'f' + text[30:]print(changed)print(text)Index 29: t
| 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 |
| Y | o | u | m | i | s | s | a | l | l | s | h | o | t | s | y | o | u | d | o | n | ' | t | t | a | k | e |
text = 'Test'letters = list(text)letters[1] = 'E'changed = ''.join(letters)print(letters)print(changed)print(text)name = ' Ada Lovelace 'words = name.split()cleaned = ' '.join(words)print(words)print(repr(cleaned))name = 'Dirk'student = {'name': 'Dirk', 'age': 21}print(f'My name is {name}')print(f"{student['name']} is {student['age']} years old")name = 'Dirk'print(f'|{name:<20}|')print(f'|{name:^20}|')print(f'|{name:>20}|')print(f'|{name:0>20}|')for number in [5, 9, 10, 15]: print(f'{number:03}')pi = 3.14159265print(f'Pi is {pi:.4f}')print(pi)ENGG1330
Computer Programming I
Key = 11
| Plain | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
| Cipher | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | a | b | c | d | e | f | g | h | i | j | k |
Each letter maps to the letter below it
h e l l o → s p w w z
plain_text = 'hello'key = 11print(plain_text)print(key)plain_text
The text to transform
key
How far to shift each letter
Use lowercase English letters and a key from 0 to 25
plain_text = 'hello'key = 11alphabet = 'abcdefghijklmnopqrstuvwxyz'shifted = alphabet[key:] + alphabet[:key]print(alphabet)print(shifted)alphabet[11:]lmnopqrstuvwxyz
alphabet[:11]abcdefghijk
Join the suffix and prefix
plain_text = 'hello'key = 11alphabet = 'abcdefghijklmnopqrstuvwxyz'shifted = alphabet[key:] + alphabet[:key]cipher_text = ''for char in plain_text: index = ord(char) - ord('a') cipher_text += shifted[index]print(cipher_text)| Variable | Value |
|---|---|
char | 'h' |
index | 7 |
shifted[index] | 's' |
cipher_text | 's' |
| Variable | Value |
|---|---|
char | 'e' |
index | 4 |
shifted[index] | 'p' |
cipher_text | 'sp' |
| Variable | Value |
|---|---|
char | 'l' |
index | 11 |
shifted[index] | 'w' |
cipher_text | 'spw' |
| Variable | Value |
|---|---|
char | 'l' |
index | 11 |
shifted[index] | 'w' |
cipher_text | 'spww' |
| Variable | Value |
|---|---|
char | 'o' |
index | 14 |
shifted[index] | 'z' |
cipher_text | 'spwwz' |
alphabet = 'abcdefghijklmnopqrstuvwxyz'cipher_text = 'spwwz'for key in range(26): shifted = alphabet[-key:] + alphabet[:-key] plain_text = '' for char in cipher_text: plain_text += shifted[ord(char) - ord('a')] print(f'{key:02}:{plain_text}', end=' ')names = []while True: new_name = input('Name (blank ends): ') if new_name == '': break names.append(new_name) print(names)names = []for new_name in ['HKU', 'CUHK', 'HKUST']: pos = 0 while pos < len(names): if names[pos] > new_name: break pos += 1 names.insert(pos, new_name) print(names)Find the first name that is greater than the new name
Insert immediately before it
If none is greater, insert at the end
names = []for new_name in ['HKU', 'CUHK', 'HKUST']: pos = 0 while pos < len(names): if names[pos] > new_name: break pos += 1 names.insert(pos, new_name) print(names)New name: 'HKU'
pos = 0
| Empty list |
Begin with an empty list
New name: 'HKU'
pos = 0
| Empty list |
0 < 0 is False
New name: 'HKU'
pos = 0
| Empty list |
Skip the while body
New name: 'HKU'
pos = 0
| 0 |
| HKU |
Insert at position 0
New name: 'HKU'
pos = 0
| 0 |
| HKU |
Print the updated list
names = []for new_name in ['HKU', 'CUHK', 'HKUST']: pos = 0 while pos < len(names): if names[pos] > new_name: break pos += 1 names.insert(pos, new_name) print(names)New name: 'CUHK'
pos = 0
| 0 |
| HKU |
Begin the next iteration
New name: 'CUHK'
pos = 0
| 0 |
| HKU |
0 < 1 is True
New name: 'CUHK'
pos = 0
| 0 |
| HKU |
'HKU' > 'CUHK' is True
New name: 'CUHK'
pos = 0
| 0 |
| HKU |
break leaves the while loop
New name: 'CUHK'
pos = 0
| 0 | 1 |
| CUHK | HKU |
Insert before HKU
New name: 'CUHK'
pos = 0
| 0 | 1 |
| CUHK | HKU |
Print the updated list
names = []for new_name in ['HKU', 'CUHK', 'HKUST']: pos = 0 while pos < len(names): if names[pos] > new_name: break pos += 1 names.insert(pos, new_name) print(names)New name: 'HKUST'
pos = 0
| 0 | 1 |
| CUHK | HKU |
Reset pos for the new name
New name: 'HKUST'
pos = 0
| 0 | 1 |
| CUHK | HKU |
0 < 2 is True
New name: 'HKUST'
pos = 0
| 0 | 1 |
| CUHK | HKU |
'CUHK' > 'HKUST' is False
New name: 'HKUST'
pos = 1
| 0 | 1 |
| CUHK | HKU |
Advance to the next position
New name: 'HKUST'
pos = 1
| 0 | 1 |
| CUHK | HKU |
1 < 2 is True
New name: 'HKUST'
pos = 1
| 0 | 1 |
| CUHK | HKU |
'HKU' > 'HKUST' is False
New name: 'HKUST'
pos = 2
| 0 | 1 |
| CUHK | HKU |
Advance again
New name: 'HKUST'
pos = 2
| 0 | 1 |
| CUHK | HKU |
2 < 2 is False
New name: 'HKUST'
pos = 2
| 0 | 1 | 2 |
| CUHK | HKU | HKUST |
Insert at the end
New name: 'HKUST'
pos = 2
| 0 | 1 | 2 |
| CUHK | HKU | HKUST |
Print the updated list
names = []for new_name in ['HKU', 'CUHK', 'HKUST']: names.append(new_name)ordered = sorted(names)print(ordered)print(names)| Select | Index, slice, or iterate |
|---|---|
| Check | Compare, search, or test characters |
| Construct | Concatenate, join, or replace |
| Present | Format values with f-strings |
A string operation can return a new value
without changing the original string
L-4 Survey
Complete the full survey during the lecture
Work independently on the L-4 challenges in Ed