Jed Rembold
September 22, 2025
| 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 |
What would be the evaluated value of
revq("Jed1s!") given the definition to the
right?
"J2s!""r3d 1 s ""r2 d2 s""r2d2s"def revq(word):
out = ""
for char in word:
if (char.isalnum() and
char.islower() ):
out += " " + char
elif char.isdigit():
out += str(int(char)+1)
elif not char.isalnum():
out = out.replace("e", "r2")
return out.strip()
| 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||| |
english.py Libraryenglish module
english module exports two
resources:
ENGLISH_WORDS: a constant sequence which
contains all the valid English words in alphabetical orderis_english_word(): a predicate function
which takes a string as input and returns
True or False
depending on if that string is a valid English wordSuppose we wanted to determine the longest word in the English language without vowels:
from english import ENGLISH_WORDS
def contains_vowels(word):
for letter in word:
if letter in "aeiou":
return True
return False
def find_longest_no_vowels():
best_length = 0
for word in ENGLISH_WORDS:
if ( not contains_vowels(word) and
len(word) > best_length ):
best_length = len(word)
print(word)
if __name__ == '__main__':
find_longest_no_vowels()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"
def pig_latin_equals_english():
count = 0
for word in ENGLISH_WORDS:
piglatin = word_2_pig_latin(word)
if is_english_word(piglatin) and word != piglatin:
print(word, ":", piglatin)
count += 1
return count
WordleGWindow
inputWe’ve seen how to display information to a user, but to retrieve
data from a user, we can use Python’s built-in
input() function
The form will generally look like:
|||variable||| = input(|||prompt_text|||)
|||variable||| is the variable name you
want to assign the user’s typed input to|||prompt_text||| is the string that
will be displayed on the screen to communicate to the user what they
should be doingThe input() function always
returns a string
If you want to get an integer from the user, you will need to convert it yourself after retrieving it
num = int(input('Pick a number between 1 and 10: '))Constructing text or sentences by interleaving strings and other objects comes up a lot in communicating code results to a user
For any Python version past 3.6, the nicest and easiest way to do this is with what are called f-strings:
A = 10
print(f"The value of A is: {A}!")You can define an f-string anytime you would normally define a string, just be aware that the substitution happens with the values of variable at that point
A = 10
s = f"The value of A is {A}"
A = 12
print(s){} placeholder
A = 10.234
print(f"The value of A is {A:0.2f}")+ sign ensures all numbers will have
either a + or -
sign in front- sign in front)<, >,
or ^ for left, right, or center
justified| Code | Description |
|---|---|
b |
Inserts an integer using its binary representation |
d |
Inserts an integer using its decimal representation |
e or E |
Inserts a number using scientific notation |
f or F |
Inserts a number using a decimal point format |
g or G |
Choose e or
f to get the best fit |
o |
Inserts an integer using its octal representation |
s |
Inserts a string value |
x or X |
Inserts an integer using its hexadecimal representation |