What I Learned In Python This Week #3: Strings, Glorious Strings!

Tools I Used:

  • Geany
    • Still my favorite mini-IDE! It comes with this awesome F5 shortcut that lets you execute the file you’re working in — great for testing!
  • Python shell / bash shell
    • I use the Python shell to learn functions interactively, and the bash shell to execute the scripts I write (though I now use Geany’s F5 shortcut more often).


  • Madlibs (madlibs.v1-0-0.py)
    • Fun Mad Libs-style game that uses template strings to get the user’s answers. Only one madlib right now, though I’d like to add more in the future.
  • ROT13 Encoder (rot13encoder.v1-0-0.py)
    • Uses the .translate method to encode user input into ROT13.
  • ROT13 Decoder (rot13decoder.v1-0-0.py)
    • Since I haven’t learned if/then statements yet, this is a separate script to decode ROT13 into readable-ish English.
  • E-mail Quotation Reformatter (emailquotationreformatter.v1-0-0.py)
    • Tool of questionable usefulness that strips excess “> > > > > ” action from overly-quoted e-mails.

Functions/Concepts I Learned:

  • String Formatting
    • It is possible to format strings in any number of ways using
    • % is the string formatting operator. % must be formatted in proper order for the conversion to work: % starts the conversion specifier; (optional) conversion flags determine sign padding; (optional) minimum field width determines what it sounds like it determines; (optional) .n determines decimal precision or maximum field width; then finally the conversion type.
    • Conversion flags are “ “, “+“, “0“, or “-“. “ ” (an empty space) means a space preceds positive numbers and a negative sign precedes negative numbers. “+” means a positive sign precedes positive numbers and a negative sign precedes negative numbers. “0” means all numbers will be zero-padded (e.g., 100 with a 5-digit zero-padding would display as 00100, 14 as 00014, etc.). “-” means the numbers will be left-aligned, rather than right-aligned.
    • Minimum field width can be either a integer or * (asterisk). If it is *, then the minimum field width will be read from the value tuple.
    • The n of .n can be either an integer or * (asterisk). If n is an integer, it determines either decimal precision or maximum field width, depending on whether a number or a string is being converted. If n is a *, then the value of n will be read from the value tuple.
    • There are many different conversion types: d or i (signed integer decimal), u (unsigned integer decimal), f or F (signed? floating point decimal), e or E (floating point with exponents), o (unsigned octal), x or X (unsigned hexadecimal), g or G (same as e/E if exponent is greater than -4 or less than .n precision, otherwise f/F), c (single character, integer or string), and r (whole string; equivalent to repr() or `` [backticks]).
  • Template Strings
    • from string import Template gives you bash-style variable substitution capabilities. Template strings are declared with a $ and the name of the variable (e.g., $foo). If the string will be inside a word, you put brackets around the variable name (e.g., ${foo}tacular!).
    • The .substitute method allows you to use these variables in your code (e.g., foobar = ("You're ${foo}tastic!") is passed a value by foobar.substitute(foo="fan"), which leads to "You're fantastic!")
    • You can also make a dictionary (which I don’t know much about yet, but implemented by guesswork in madlibs.v1-0-0.py) that calls the $ substitutions. This will eventually show up as something like foobar.substitute(dictionary) where dictionary would contain a ["foo"] = "fan" entry.
    • If you need to display $ in your template string, put extra an extra $ in front of each $ you want to display (e.g., $$ to print $, but $$$$ to print $$, etc.).
  • String Methods
    • String methods are analgous to the list methods I learned in week 2, only for strings 😉 . The general format for string methods is [called string or declared string].[method]([called list or declared list]) — meaning there are multiple functionally equivalent ways to use string methods.
    • .find finds 😉 the first instance of a search term in a string (analgous to .index or in). It returns the leftmost index number if the search term is found, otherwise it returns -1 to indicate no matches. As in .index, only the first matching instance is returned. .find can be limited by giving it a start point, or a start and end point (e.g., foo.index("foobar",0,99)) would look for “foobar” in the first 100 indices of foo).
    • .join joins the elements in a sequence. Because this is a string method, the elements it joins must be string elements, otherwise .join will fail from a syntax error. .split does the reverse, splitting a list into a sequence. If no separator is specified, .split will split strings along all whitespace characters.
    • .lower, .upper, .title, .capitalize, and .swapcase are various ways to format the text in a string. .lower makes all characters in a string lowercase, .upper makes them all uppercase, .title makes them all title case, .capitalize makes the first letter uppercase and the rest lowercase, and .swapcase reverses the case of each letter in the string. .islower, .isupper, and .istitle are Boolean methods to determine whether a string is in lower, upper, or title case respectively, and will return either True or False. When using any of these methods, you must call them with empty parentheses, otherwise you’ll get an error message.
    • .replace replaces all instances of a search term with another substring. .translate works similarly to .replaces, only .translate deals with single characters. .translate basically makes a custom 256-character ASCII mapping table that it runs strings through. You can also specify characters to be deleted (e.g., with a unchanged ASCII map, "Make me happy!".translate(transtable, ' ') yields 'Makemehappy!').
    • .strip strips all left and right trailing whitespace, but not internal whitespace. You can also specify other characters to be stripped by listing them as a string (e.g., "> > > > This e-mail has been forwarded way too many times".strip(' <') results in "> > > > This e-mail has been forwarded way too many times".strip(' <')). .lstrip and .rstrip work similarly, working with only the left or right trailing characters respectively.
  • string.maketrans() generates a translation table which can be used with the .translate method.
  • Also: in checks whether something is a member of a list or a string. It returns True or False depending on the result of the check. [I realize I should have put this one up last week, but for some reason my brain totally skipped it.]

This week was really fun, though it was quite a brain-buster getting through the whole chapter on string formatting in Hetland. I think my favorite Python quality is that it lets me learn concepts and implement them soon afterwards — although it is at the expense of letting me know exactly what is going on. I’m thinking of learning C next so I can appreciate the lower levels of what I’m programming, but with the knowledge of the greater scope of what programming is all about from Python.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s