CS 151: Lab #4

Lab Exercise 4: User Input and Strings

The purpose of this lab time is to give you more practice with python and writing programs. The focus this week will be on getting input from a user and working with strings.


  1. Connect to your network directory.
  2. Create a new file in BBEdit or whatever editor you wish. Save the file as build.py.
  3. Define a main function with no arguments. Assign a string to a variable. Name the variable whatever you like, use whatever string you like. Have the main function print out the string contained in the variable. Test the program.
  4. Create a function called strmerge() that takes two parameters. Given two strings, the program should interleave the characters and return the merged string. Use a for loop that iterates over the longer of the two strings to execute the merge. Within the for loop, the program should alternate concatenating characters onto a new string. Use a return statement to return the new string. Test the program by calling it with two strings from your main function and printing out the result.

    As an example, try merging the strings '12345' and 'abcdefg'. The result should be the string '1a2b3c4d5efg'.

  5. In your main program, ask the user for two strings. For each string, prompt the user for a string using a print statement, then use the raw_input() function to get the string from the user and assign it to a variable. Combine the two strings using your strmerge() function, then print out the result. Test your program and see if it works.
  6. Now we want to write the merged string to a file. To open a file, create a file object using file(filename, mode). The function takes two arguments: the first is the filename, the second indicates how you want to use the file. If you want to read from the file, put 'r' as the second argument. If you want to write to the file, use 'w' as the second argument. If the file does not exist, using the 'w' argument will create the file.

    fp = file('myfilename', 'w')

  7. The variable fp now holds a file object that was created by the call to file(). Note that what you name the variable on the left side of the assignment is completely arbitrary. I tend to use the variable name fp out of habit. A file object is a data type just like float, or int, or str. However, unlike the basic data types, an object may have functions associated with it through which you can tell the object what to do. In the case of a file object, it has functions that know how to read and write data to the file and a function that knows how to close the file.

    Remember, if you want to know the type of data stored in a variable, just use the type() function and pass it the variable. Try it with the variable that holds the object returned by the call to file().

    If the type of data in a variable is unknown to you, python has a function help() that takes a type (like file, or float) and tells you all of the things that object or type of data can do. Try importing the turtle package using import turtle and then try out the help function help(turtle).

    Getting back to writing a string to a file, the file object has a method (or function) called write() that takes a string as an argument and writes it to the file.

    fp.write('string to writef\n')

    You can write the result of the merge operation by simply passing the variable with the string in it to the write function. In the example above, the '\n' is a newline character, which simply means the file will have a newline at the end of it.

  8. Close the file by calling the file object function close()


    It is important to always close a file when you are done with it. Otherwise, it is possible that data you are trying to write to the file won't be saved. Now try running your program and see if the file is there when you are done. What is in the file? You can use the cat command on the command line to dump the contents of the file to the terminal.

  9. The last step is to modify your linear.py file to read in a string from the file you wrote out previously. First, make a copy of your linear.py file and call it linearB.py, then open linearB.py in an editor. In the main function, before your call to processString, modify it to open the file you created for reading. Then use the readline() method (function) of the file object to read the string from the file. The readline() returns a single line of the file as a string and advances the file location to the beginning of the next line.

    aString = fp.readline()

    You can call the variable that receives the string from readline() whatever you like. I'm called it aString in the example above. As the final step, pass the variable that holds the string into the processString function.

  10. The last step is to test the whole process. Run the build.py program to create a string and store it in a file. Then run linearB.py to read the string from the file and interpret the string to draw a string of shapes.

Once you've finished the above exercise, get started on the next project.