-
Notifications
You must be signed in to change notification settings - Fork 806
/
023_string_indexing.py
125 lines (87 loc) · 2.5 KB
/
023_string_indexing.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# Video alternative: https://vimeo.com/954334279/dd2abfbdd7#t=410
from lib.helpers import check_that_these_are_equal
# In the earlier material, we focused on numbers.
# Numbers are a very simple data type — they represent a
# single value.
# Strings are more complex. They're a basic form of what, in
# programming, we call a 'data structure'.
# This means they've got more than one thing in them, and
# they're organised in a specific way depending on what type
# of data structure they are.
# A string has a number of characters inside it in a
# particular order.
# Take this string:
note = "The Most Perfect Crab"
print(note)
# We can access the first character like this:
print(note[0])
# In programming, we count from zero — 'T' is the zeroth
# character.
# And the last character like this:
print(note[-1])
# And any in the middle like this:
print(note[6])
# You can also get a 'slice' of the string like this:
print(note[0:3])
# This gets the portion of the string between index 0 and 3:
# 'The'
# @TASK: Complete the following exercises. You can check
# them as you go by running: python 023_string_indexing.py
# == Exercise One ==
print("")
print("Function: get_first_letter")
def get_first_letter(the_str):
# Return the first letter of the string
pass
check_that_these_are_equal(
get_first_letter("The king granted them"),
"T"
)
check_that_these_are_equal(
get_first_letter("Five years later"),
"F"
)
# == Exercise Two ==
print("")
print("Function: get_last_letter")
def get_last_letter(the_str):
# Return the last letter of the string
pass
check_that_these_are_equal(
get_last_letter("The king granted them"),
"m"
)
check_that_these_are_equal(
get_last_letter("Five years later"),
"r"
)
# == Exercise Three ==
print("")
print("Function: get_nth_letter")
def get_nth_letter(the_str, n):
# Return the letter of the string at the specified index
pass
check_that_these_are_equal(
get_nth_letter("The king granted them", 4),
"k"
)
check_that_these_are_equal(
get_nth_letter("Five years later", 7),
"a"
)
# == Exercise Four ==
print("")
print("Function: get_letters_between_four_and_eight")
def get_letters_between_four_and_eight(the_str):
# Return the section of the string between indexes four
# and eight
pass
check_that_these_are_equal(
get_letters_between_four_and_eight("The king granted them"),
"king"
)
check_that_these_are_equal(
get_letters_between_four_and_eight("Five years later"),
" yea"
)
# When you're done, move on to 024_string_operations.py