Jed Rembold
October 10, 2022
Suppose you have the string
x = "consternation" and you’d like to just
extract and print the word "nation". Which
expression below will not give you the string
"nation"?
x[7:len(x)]x[7:]x[-6:len(x)]x[-6:-1]| 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"