Formatting Strings

Jed Rembold & Fred Agbo

February 7, 2025

Announcements

  • Problem set 3 is due next week Monday at 10 pm
  • I am working on returning PS 2. You should get it hopefully, by next week
  • Our class on Friday February 16 will be at Eaton 311
  • Remember the only way to master coding is to keep practicing
  • Link to Polling https://www.polleverywhere.com/agbofred203

Review!

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"?

  1. x[7:len(x)]
  2. x[7:]
  3. x[-6:len(x)]
  4. x[-6:-1]

Transforming Methods

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

Classifying Character Methods

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

Igpay Atinlay

  • Suppose we wanted to write a script that converted English to Pig Latin
  • Rules of Pig Latin:
    • If the word begins with a consonant, move everything up to the first vowel to the end and append on “ay” at the end
      fleeteetflay
    • If the word starts with a vowel, just append “way” to the end
      orangeorangeway
    • If the word has no vowels, do nothing
  • Our decomposition:
    • Find first vowel
    • Convert a single word

Indingfay Owelsvay

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

Onvertcay Oneway Ordway

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"

Definitely not G-Strings

  • 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)

Formatting Strings

  • F-strings allow up to easily substitute in values, but what if we want those values to have a particular format?
    • Rounded to the nearest two decimal places, for example
  • One option would be to handle all this before the substitution manually
  • Python gives a more streamlined method, using a format spec
  • A format spec will be given inside the {} placeholder
    • Comes after the variable/value itself
    • There is a colon between the variable/value and the format spec
    A = 10.234
    print(f"The value of A is {A:0.2f}")

Shaping your format

  • A Format spec is a special string that determine the desired format
  • Lots we can do, but we rarely need to do it all at once
  • The basic building blocks (square brackets just to group):

    [[fill]align][sign][width][,][.precision][type]

  • Type
    • How you want the object represented as a string
    • “d” - base-10 integer
    • “f” - float or decimal
    • “e” - scientific notation
    • More on next slide
  • Precision
    • How many digits to display after a decimal point
    • Details can vary a little by conversion type
  • Grouping?
    • A comma here indicates that numbers should be grouped in sets of 3 and separated with a comma
  • Width
    • The minimum number of characters you want the formatted value to have
    • If not otherwise specified, pads the value with space characters
  • Sign?
    • If the sign of the number should be specified
    • A + sign ensures all numbers will have either a + or - sign in front
    • A space just adds a space before positive numbers (negatives would have a - sign in front)
  • Fill and Align
    • How you want the text aligned if it is shorter than the minium width
      • Can be <, >, or ^ for left, right, or center justified
    • Any fill characters you want to fill the empty space come before
      • Default is space

Output Conversion Types

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
  • Uppercase conversion types use capital characters where possible in output

Learning English

  • When working with sequences of characters, it is often useful or desirable to determine if they form actual valid English words
  • This class provides for you a new library, through the file english.py
  • The english library provides two objects you can import into your programs:
    • The constant ENGLISH_WORDS, which is a list of all the valid words in the English dictionary
    • The function is_english_word(), which accepts a single string as an argument and returns True if the string represents a valid English word.
  • This library will be particularly useful for Wordle!

Example: How many 2 letter words?

  • Before we start writing code, let’s pause. Give a physical English dictionary, how could you go about figuring out the number of two letter words?
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)
// reveal.js plugins