Jed Rembold & Fred Agbo
February 9, 2024
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"
Constructing text or a sentence by interleaving strings and other objects comes up a lot in communicating code results to a user
We’ve already seen that, for any version past 3.6, the nicest and easiest way to do this in Python is with 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
justifiedCode | 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 |
english.py
english
library provides two objects
you can import into your programs:
ENGLISH_WORDS
, which is a
list of all the valid words in the English dictionaryis_english_word()
, which
accepts a single string as an argument and returns
True
if the string represents a valid
English word.from english import ENGLISH_WORDS
count = 0
for word in ENGLISH_WORDS:
if len(word) == 2:
count += 1
print(count)
from english import is_english_word
count = 0
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for letter1 in alphabet:
for letter2 in alphabet:
word = letter1 + letter2
if is_english_word(word):
count += 1
print(count)