Learning Objectives:

Software tools needed: web browser, Python IDLE programming environment and Unix shell.

Download the Skeletal Notes and Focus Questions to guide you while studying this lab.
These are a useful tool for note taking and you can keep these handy to study for and refer to during the final exam.


At the end of this lab, don't forget to take Lab Quiz 2! See the quiz page for details of the work due this week.

Using Python, Gradescope, and Blackboard

See Lab 1 for details on using Python, Gradescope, and Blackboard.


A sequence of characters (i.e. letters, numbers, symbols) is called a string. To indicate a string, we use quotes (either single or double, just as long as they match up) to indicate the start and end (a fancier way to say that is: quotes deliminate the string). For example, for our first program, we wrote:

print("Hello, World!")

The string, or sequence of characters, Hello, World!, was printed to the screen. We can also store strings to be used again in the program. For example,

greeting = "Hello, World!"

creates a location in memory that can be accessed by typing the name (or identifier) that we chose: greeting. greeting stores the string Hello, World!. While the quotes are not stored, we will often write "Hello, World!" to make it more clear where the string begins and ends. When we execute the code above, the first line will create the variable, greeting and store the string "Hello, World!" in it. The second line will print out the message:

Hello, World!

We can use the variable any number of times. For example, if we wanted to print the message twice, we could use:

greeting = "Hello, World!"

More Useful String Methods

Since strings are used everywhere, there are many built-in functions for strings.

For historic reasons, we start counting at 0, instead of 1, in many computer languages, including Python. When you use find() command on the string ``Hello, World!'', the first character is at 0, the next character at 1, etc.

Hello, World!

The find() command gives the location of "ll" which is 2 if you start by counting the first character as 0.

Getting Input

Last week, we used the print() function to write messages to the user of our program. This week, we introduce, input(), to get information from the user. Here's the basic format:

aString = input("Put a message here to show user: ")
where the string "Put a message..." is replaced by the prompt you would like the user to see and aString with the name of the string you are using in your program.

Let's write a program that combines the asking the user for input with the string commands a the beginning of the lab. The program will:

  1. Prompt the user for a message and store it in the variable, mess.
  2. Print the message to the screen.
  3. Print the message in all capital letters.
  4. Print the message in all lower case letters.

To start, open IDLE and start a new file window. Put a comment (lines that begin with '#') that includes your name and a short description of what the program does.

  1. Next, fill in the code that prompt the user for a message and store it in the variable, mess (see the example above).
  2. Print the message to the screen (also done in the example above).
  3. To print in all capital letters, you can use the upper() command (see first example in the lab).
  4. Print the message in all lower case in a similar way.

Save your file as you go, and then run it. Try different messages to make sure it works with different inputs. When it works add your name in a comment with a short description of what it does and see the Programming Problem List.

Looping Through Strings

You can also loop through strings. Try running the code below by clicking the Next button to execute each instruction:

Each character has a number assigned to it. When you write a character, it is converted to its number, and that is stored instead to save space. Python uses the standard Unicode encoding (which extends the popular ASCII encoding to new symbols and alphabets). For example, ord('a') give the Unicode number for the character, a, which is 97.

Let's look at the Unicode of the characters in our string:

To go the other direction, there's a function chr() which takes numbers and returns the corresponding character. For example, chr(97) returns 'a'. Let's look at the characters with unicode from 65 to 69:

Combining ord() and chr() demonstrated in the two programs above, modify the first of the two programs to:

For example, for character 'a' with Unicode number 97, it prints:
  a 98 b
When it works, add in your name in a comment, and see the Programming Problem List.

The range() statement has several different options:

Looping Through Lists

You can loop through a list the same way as you loop through a string.

The split() function breaks up a string into substrings, throwing away the character or delimiter on which the function splits.

Then you can loop through the list just like you loop through a string to access the individual substrings.

For example, given the string "FridaysSaturdaysSundays", split('s') will break up the string into a list of days: ["Friday","Saturday","Sunday",""] throwing away the 's' characters on which it splits.

Notice that it will produce an empty string at the end because of the 's' character at the end of "Sundays".

Try running the code below by clicking the Next button to execute each instruction:

Modify the code to remove the 's' at the end of 'Sundays', now run and inspect the list. What changed?

Biomolecular Sequences

Let's apply what we just learned to some questions from biology. DNA is a molecule that contains instructions for the cell (wiki). We can represent it as a string of four characters: 'A', 'C', 'G', and 'T' corresponding to the four nucleotides that are the building blocks for the sequences. For example,

is the start of the DNA sequence for insulin in humans.

We have the tools to compute how long the out sequence is as well as GC-content (the fraction of the sequence that is C and G) which is correlated with the stability of the molecule:

Indexing: looping Through Strings, Revisited

There's another way we can loop through strings, using the index of each character. Let's assume that we have:

greeting = "Hello, World!"
Before, we printed out the whole string with:
If we wanted to print out only the first letter, we could write:
where the number between the square brackets is the index of the character, in this case, 0, or the very first character of the string.

Try guessing what the following code does and then running it by pressing the Next button to execute each instruction:


It is often quite useful to break a string into pieces, and work with those pieces, instead of the whole string.

Based on the programs demoed above, combine the use of split() and indexing (the use of square brackets to access individual characters in a string using the character's index) to write a program that takes a phrase and creates an acronym of the phrase: that is, the first letter of each word.

To approach a problem, it is useful to break it into steps:

  1. Prompt for a phrase & read it in.
  2. Make the phrase upper case.
  3. Split up the phrase into words.
  4. Take the first letter of each word (keep in mind that split() returns a list of the words ), concatenate and make an acronym of it
To concatenate strings you can use the + operator. For example:
will output:
Now translate the above pseudocode (informal but detailed description of the steps in a program) into python and test that your program works as follows:
Enter a phrase:  City University New York
Your phrase in capital letters:  CITY UNIVERSITY NEW YORK
Acronym:  CUNY
When it works, add in your name in a comment, and see the Programming Problem List.

Files & the Command Line Interface

The terminal is a program that allows users to type commands (separate from Python) that we can use to communicate with the operating system. It's often called the command line interface or shell. It predates the graphical interface that is now available and is incredibly useful for automating tasks and working on remote servers. We will slowly introduce shell commands over the course of the semester and eventually incorporate them into some programming assignments.

To access a Unix shell environment on your computer to run shell commands:

We have already used the shell in the previous lab. When you typed:

$  idle
you were asking the operating system to launch the program idle3.

Let's create a new directory, or folder, to hold your images (using the shell or command line). Where it says thomasH below, replace with your name. It is easier if you avoid spaces in the names:

The graphical interface and shell are operating on the same underlying system, so, changes you make with one can be seen by the other. If you use your OS' graphical user interface to open the folder (directory) you started in when you typed pwd, you will see the folder you just created.

In the next lab, we will explain how to copy and move files between directories.

Installing the Windows Subsystem for Linux

If you don't have a Windows machine, you can move on to the Lab Quiz

If you are using Windows, here's directions on how to get the Linux terminal on your computer:

  1. Search "Turn Windows features on or off" in the Windows search bar and open it.
  2. Look for "Windows Subsystem for Linux", make sure the box is checked, and hit OK (You will have to restart your computer).
  3. Open Microsoft Store application and search Ubuntu.
  4. Click on Ubuntu, hit get, and wait for it to install (You can use any version, the one without a version number will give you the latest version of Ubuntu).
  5. Launch the Ubuntu application and allow first time setup.
  6. Create a username and password (When entering a password, no characters will appear, but you are typing your password).
  7. Update Ubuntu with the command:
     sudo apt update && sudo apt upgrade 
    (You will have to enter the password you set) You should run this command periodically as Ubuntu will not update on its own.

You have successfully installed and setup WSL and can now use the terminal as you would on Ubuntu.

These installation instructions were written by Owen Kunhardt. You can find the full guide here.

What's Next?

You can start working on this week's programming assignments. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.
Keep in mind that the due dates are one week late for flexibility (if one week there is a setback and you can't submit your programs, you will have time catch up). Still, each week you should work on the programming assignments for that week, even if they are due a week later. If you are on a roll, you are welcome to work ahead!!!