Jed Rembold & Fred Agbo
February 5, 2025
How would you represent the number \[28_{16}\] in binary?
'I am a string'
"I am also a string!"
"I'm sad you've gone"
[
, ]
) with
commas separating each element of the sequence
['This', 'is', 'a', 'list']
['Great', 4, 'storing', 5 * 10]
[
]
>>> A = [2, 4, 6, 8]
>>> print(A[1])
4
>>> B = "Spaghetti"
>>> print(B[6])
't'
+
operator concatenates sequences
+
will add two integers,
but will concatenate two strings>>> 'fish' + 'sticks'
'fishsticks'
>>> A = [1, 'fish']
>>> B = [2, 'fish']
>>> print(A + B)
[1, 'fish', 2, 'fish']
The number of elements in a sequence is commonly called its
length, and can be given by the
len( )
function
Simply place the sequence you desire to know the length of between the parentheses:
>>> len("spaghetti")
9
You can have sequences of 0 length as well!
>>> A = ""
>>> B = [ ]
>>> print( len(A) + len(B) )
0
chr
and
ord
chr
takes a base-10 integer and returns
the corresponding Unicode character as a string
chr(65)
gives
"A"
(capital A)chr(960)
gives
"π"
(Greek letter pi)ord
goes the other direction, taking a
single character string and returning the corresponding base-10 integer
of that character in Unicode
ord("B")
gives 66ord(" ")
gives 32ord("π")
gives 960You can select individual characters from the string using the syntax
string[k]
where string
is the variable assigned to
the desired string and k
is the index
integer of the character you want
>>> print("spaghetti sauce"[5])
e
A common use case is to grab the last character of the string, using
s[-1]
which is shorthand for
s[len(s)-1]
Often, you may want more than a single character
Python allows you to specify a starting and an ending index through an operation known as slicing
The syntax looks like:
string_variable[start : limit]
where start
is the first index to be
included and everything up to but not including the
limit
is included
start
and
limit
are actually optional (but the
:
is not)
start
omitted, the slice will begin
at the start of the stringlimit
omitted, the slice will proceed
to the end of the stringCan add a third component to the slice syntax, called a stride
string_variable[start : limit : stride]
Specifies how large the steps are between each included index
Can also make the stride negative to proceed backwards through a string
>>> s = "spaghetti sauce"
>>> s[4:8]
hett
>>> s[10:]
sauce
>>> s[:10:2]
sahti
+
) in Python to concatenate strings\[5+5+5+5+5+5 = 6 \times 5\]
print("Betelguese, " * 3)
Python lets you use normal comparison operators to compare strings
string1 == string2
is true if string1
and
string2
contain the same characters in the
same order
Comparisons involving greater than or less than are done similar to alphabetical ordering
All comparisons are done according to their Unicode values.
"cat" > "CAT"
Strings are what we call immutable: they can not be modified in place by clients.
You can “look” at different parts of the string, but you can not “change” those parts without making a whole new string
s = "Cats!"
s[0] = "R" # THIS WILL ERROR!!
You can of course create a new string object with the desired traits:
s = "R" + s[1:]
This applies to all methods that act on strings as well: they return a new string, they do not modify the original
Method | Description |
---|---|
string.find(pattern) |
Returns the first index of pattern in
string , or -1 if
it does not appear |
string.find(pattern, k) |
Same as the one-argument version, but starts searching at index
k |
string.rfind(pattern) |
Returns the last index of pattern is
string , or -1 if
missing |
string.rfind(pattern, k) |
Same as the one-argument version, but searches backwards from index
k |
string.startswith(prefix) |
Returns True if the string starts with
prefix |
string.endswith(suffix) |
Returns True if the string ends with
suffix |
Method | Description |
---|---|
string.lower() |
Returns a copy of string with all
letters converted to lowercase |
string.upper() |
Returns a copy of string with all
letters converted to uppercase |
string.capitalize() |
Returns a copy of string with the first
character capitalized and the rest lowercase |
string.strip() |
Returns a copy of string with whitespace
and non-printing characters removed from both ends |
string.replace(old, new) |
Returns a copy of string with all
instances of old replaced by
new |
Method | Description |
---|---|
char.isalpha() |
Returns True if
char is a letter |
char.isdigit() |
Returns True if
char is a digit |
char.isalnum() |
Returns True if
char is letter or a digit |
char.islower() |
Returns True if
char is a lowercase letter |
char.isupper() |
Returns True if
char is an uppercase letter |
char.isspace() |
Returns True if
char is a whitespace character (space, tab,
or newline) |
char.isidentifier() |
Returns True if
char is a legal Python identifier |
fleet
⟶ eetflay
orange
⟶
orangeway
def find_first_vowel_index(word):
"""
Find the first vowel in a word and return its index,
or return None if no vowels found.
"""
for i in range(len(word)):
index = "aeiou".find(word[i].lower())
if index != -1:
return i
return None
def word_2_pig_latin(word):
"""
Convert a single word with no special characters from
English to Pig Latin.
"""
vowel = find_first_vowel_index(word)
if vowel is None:
return word
elif vowel == 0:
return word + "way"
else:
return word[vowel:] + word[:vowel] + "ay"