PYTHON - MODULE - 3 Notes
PYTHON - MODULE - 3 Notes
➢ Since the string begins with a double quote, Python knows that the single quote is part of the string
and not marking the end of the string.
Escape Characters
➢ If you need to use both single quotes and double quotes in the string, you’ll need to use escape
characters.
➢ An escape character consists of a backslash (\) followed by the character you want to add to the string.
➢ Python knows that the single quote in Bob\'s has a backslash, it is not a single quote meant to end the
string value. The escape characters \' and \" allows to put single quotes and double quotes inside your
strings, respectively.
Example:
➢ The different special escape characters can be used in a program as listed below in a table.
Page 1
Module 3 Python
Raw Strings
You can place an r before the beginning quotation mark of a string to make it a raw string. A raw string
completely ignores all escape characters and prints any backslash that appears in the string
➢ Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.
Program
Output
➢ The following print() call would print identical text but doesn’t use a multiline string.
Multiline Comments
➢ While the hash character (#) marks the beginning of a comment for the rest of the line.
➢ A multiline string is often used for comments that span multiple lines.
Page 2
Module 3 Python
➢ The space and exclamation point are included in the character count, so 'Hello world!' is 12 characters
long.
➢ If we specify an index, you’ll get the character at that position in the string.
➢ If we specify a range from one index to another, the starting index is included and the ending index is
not.
➢ The substring we get from spam[0:5] will include everything from spam[0] to spam[4], leaving out the
space at index 5.
Note: slicing a string does not modify the original string.
The in and not in Operators with Strings
➢ The in and not in operators can be used with strings just like with list values.
➢ An expression with two strings joined using in or not in will evaluate to a Boolean True or False.
Page 3
Module 3 Python
➢ These expressions test whether the first string (the exact string, case sensitive) can be found within the
second string.
➢ These methods do not change the string itself but return new string values.
➢ If we want to change the original string, we have to call upper() or lower() on the string and then
assign the new string to the variable where the original was stored.
➢ The upper() and lower() methods are helpful if we need to make a case-insensitive comparison.
In the following small program, it does not matter whether the user types Great, GREAT, or grEAT,
because the string is first converted to lowercase.
Program
Output
➢ The isupper() and islower() methods will return a Boolean True value if the string has at least one letter
and all the letters are uppercase or lowercase, respectively. Otherwise, the method returns False.
Page 4
Module 3 Python
➢ Since the upper() and lower() string methods themselves return strings, you can call string methods on
those returned string values as well. Expressions that do this will look like a chain of method calls.
Page 5
Module 3 Python
➢ The isX string methods are helpful when you need to validate user input.
➢ For example, the following program repeatedly asks users for their age and a password until they
provide valid input.
Program
output
Page 6
Module 3 Python
➢ These methods are useful alternatives to the == equals operator if we need to check only whether
the first or last part of the string, rather than the whole thing, is equal to another string.
➢ string join() calls on is inserted between each string of the list argument.
➢ Ex: when join(['cats', 'rats', 'bats']) is called on the ', ' string, the returned string is 'cats, rats, bats'.
join() is called on a string value and is passed a list value.
Split()
➢ The split() method is called on a string value and returns a list of strings.
➢ We can pass a delimiter string to the split() method to specify a different string to split upon.
➢ A common use of split() is to split a multiline string along the newline characters.
Page 7
Module 3 Python
➢ Passing split() the argument '\n' lets us split the multiline string stored in spam along the newlines and
return a list in which each item corresponds to one line of the string.
Justifying Text with rjust(), ljust(), and center()
➢ The rjust() and ljust() string methods return a padded version of the string they are called on, with
spaces inserted to justify the text.
➢ The first argument to both methods is an integer length for the justified string.
➢ 'Hello'.rjust(10) says that we want to right-justify 'Hello' in a string of total length 10. 'Hello' is five
characters, so five spaces will be added to its left, giving us a string of 10 characters with 'Hello'
justified right.
➢ An optional second argument to rjust() and ljust() will specify a fill character other than a space
character.
➢ The center() string method works like ljust() and rjust() but centers the text rather than justifying it to
the left or right.
➢ These methods are especially useful when you need to print tabular data that has the correct spacing.
➢ In the below program, we define a printPicnic() method that will take in a dictionary of information
and use center(), ljust(), and rjust() to display that information in a neatly aligned table-like format.
o The dictionary that we’ll pass to printPicnic() is picnicItems.
Page 8
Module 3 Python
Program output
➢ The lstrip() and rstrip() methods will remove whitespace characters from the left and right ends,
respectively.
➢ Optionally, a string argument will specify which characters on the ends should be stripped.
➢ Passing strip() the argument 'ampS' will tell it to strip occurences of a, m, p, and capital S from the
ends of the string stored in spam.
➢ The order of the characters in the string passed to strip() does not matter: strip('ampS') will do the
same thing as strip('mapS') or strip('Spam').
▪
Copying and Pasting Strings with the pyperclip Module
➢ The pyperclip module has copy() and paste() functions that can send text to and receive text from
your computer’s clipboard.
Page 9
Module 3 Python
➢ Of course, if something outside of your program changes the clipboard contents, the paste() function
will return it.
Page 10
Module 3 Python
➢ This new code looks in the PASSWORDS dictionary for the account name. If the account name is a
key in the dictionary, we get the value corresponding to that key, copy it to the clipboard, and print a
message saying that we copied the value. Otherwise, we print a message saying there’s no account with
that name.
➢ On Windows, you can create a batch file to run this program with the win-R Run window. Type the
following into the file editor and save the file as pw.bat in the C:\Windows folder:
Page 11
Module 3 Python
➢ With this batch file created, running the password-safe program on Windows is just a matter of pressing
win-R and typing pw <account name>.
Program output
Page 12
Module 3 Python
➢ We split the text along its newlines to get a list in which each item is one line of the text. For each line,
we add a star and a space to the start of the line. Now each string in lines begins with a star.
➢ When this program is run, it replaces the text on the clipboard with text that has stars at the start of
each line.
Page 13
Module 3 Python
➢ Variables are a fine way to store data while your program is running, but if you want your data to persist
even after your program has finished, you need to save it to a file. You can think of a file’s contents as a
single string value, potentially gigabytes in size.
➢ Files and File Paths
➢ A file has two key properties:a filename (usually written as one word) and a path.
➢ The path specifies the location of a file on the computer.
➢ For example, there is a file on my Windows 7 laptop with the filename projects.docx in the path
C:\Users\asweigart\Documents.
➢ The part of the filename after the last period is called the file’s extension and tells you a file’s type.
project.docx is a Word document, and Users, asweigart, and Documents all refer to folders (also called
directories).
➢ Folders can contain files and other folders. For example, project.docx is in the Documents folder, which
is inside the asweigart folder, which is inside the Users folder.
➢
➢ Backslash on Windows and Forward Slash on OS X and Linux
➢ On Windows, paths are written using backslashes (\) as the separator between folder names.
➢ OS X and Linux, however, use the forward slash (/) as their path separator.
➢ If you want your programs to work on all operating systems, you will have to write your Python scripts
to handle both cases.
➢ This can be accomplished by the os.path.join() function. If you pass it the string values of individual
file and folder names in your path, os.path.join() will return a string with a file path using the correct
path separators.
➢ Enter the following into the interactive shell:
➢ These interactive shell examples running on Windows, so os.path .join('usr', 'bin', 'spam') returned
'usr\\bin\\spam'. (Notice that the back- slashes are doubled because each backslash needs to be escaped
by another backslash character.)
➢ If I had called this function on OS X or Linux, the string would have been 'usr/bin/spam'.
Page 14
Module 3 Python
➢ The os.path.join() function is helpful if you need to create strings for filenames. These strings will be
passed to several of the file-related functions
➢ The following example joins names from a list of filenames to the end of a folder’s name:
➢
➢ The Current Working Directory
➢ Every program that runs on your computer has a current working directory, or cwd.
➢ Any filenames or paths that do not begin with the root folder are assumed to be under the current
working directory.
➢ You can get the current working directory as a string value with the os.getcwd() function and change
it with os.chdir(). Enter the following into the interactive shell:
➢
➢ Here, the current working directory is set to C:\Python34, so the file- name project.docx refers to
C:\Python34\project.docx.
➢ When we change the current working directory to C:\Windows, project.docx is interpreted as C:\
Windows\project.docx.
➢ Python will display an error if you try to change to a directory that does not exist.
➢
➢ While folder is the more modern name for directory, note that current working directory (or just
working directory) is the standard term, not current working folder.
➢ Absolute vs. Relative Paths
➢ There are two ways to specify a file path.
➢ An absolute path, which always begins with the root folder
➢ A relative path, which is relative to the program’s current working directory
➢ There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can
be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two
periods (“dot-dot”) means “the parent folder.”
Page 15
Module 3 Python
➢ Figure is an example of some folders and files. When the current working directory is set to C:\bacon,
the relative paths for the other folders and files are set as they are in the figure.
➢ The .\ at the start of a relative path is optional. For example, .\spam.txt and spam.txt refer to the same
file.
➢ This will create not just the C:\delicious folder but also a walnut folder inside C:\delicious\walnut.
➢ That is, os.makedirs() will create any necessary intermediate folders in order to ensure that the full
path exists. Figure shows this hierarchy of folders.
➢
The os.path Module
➢ The os.path module contains many helpful functions related to filenames and file paths. Since os.path
is a module inside the os module, you can import it by simply running import os.
➢ Handling Absolute and Relative Paths
➢ The os.path module provides functions for returning the absolute path of a relative path and for
checking whether a given path is an absolute path.
Page 16
Module 3 Python
➢ Calling os.path.abspath(path) will return a string of the absolute path of the argument. This is an
easy way to convert a relative path into an absolute one.
➢ Calling os.path.isabs(path) will return True if the argument is an absolute path and False if it
is a relative path.
➢ Calling os.path.relpath(path, start) will return a string of a relative path from the start path
to path. If start is not provided, the current working directory is used as the start path.
Try these functions in the interactive shell:
➢ Since C:\Python34 was the working directory when os.path.abspath() was called, the “single-dot”
folder represents the absolute path 'C:\\Python34'.
➢ Enter the following calls to os.path.relpath() into the interactive shell:
➢ Calling os.path.dirname(path) will return a string of everything that comes before the last slash in the
path argument. Calling os.path.basename(path) will return a string of everything that comes after the
last slash in the path argument. The dir name and base name of a path are outlined in Figure
➢
➢ If you need a path’s dir name and base name together, you can just call os.path.split() to get a tuple
value with these two strings, like so:
Page 17
Module 3 Python
➢
➢ Notice that you could create the same tuple by calling os.path.dirname()and os.path.basename()
and placing their return values in a tuple.
➢
➢ Finding File Sizes and Folder Contents
➢ Once you have ways of handling file paths, you can then start gathering information about specific
files and folders. The os.path module provides functions for finding the size of a file in bytes and
the files and folders inside a given folder.
➢ Calling os.path.getsize(path) will return the size in bytes of the file in the path argument.
➢ Calling os.listdir(path) will return a list of filename strings for each file in the path
argument. (Note that this function is in the os module, not os.path.) Here’s what I get when I try
these functions in the interactive shell:
➢ As you can see, the calc.exe program on my computer is 776,192 bytes in size, and I have a lot of
files in C:\Windows\system32. If I want to find the total size of all the files in this directory, I can use
os.path.getsize() and os.listdir() together.
o As we loop over each filename in the C:\Windows\System32 folder, the totalSize variable is
incremented by the size of each file.
o When os.path.getsize()is called, os.path.join() is used to join the folder name with
the current filename.
o The integer that os.path.getsize() returns is added to the value of totalSize.
o After looping through all the files, totalSize is printed to see the total size of the
C:\Windows\System32 folder.
Page 18
Module 3 Python
➢
➢ The File Reading/Writing Process
➢ Once you are comfortable working with folders and relative paths, you’ll be able to specify the
location of files to read and write. The functions covered in the next few sections will apply to plaintext
files.
➢ Plaintext files contain only basic text characters and do not include font, size, or color information.
Text files with the .txt extension or Python script files with the .py extension are examples of plaintext
files. These can be opened with Windows’s Notepad or OS X’s TextEdit application. Your programs
can easily read the contents of plaintext files and treat them as an ordinary string value.
➢ Binary files are all other file types, such as word processing documents, PDFs, images, spreadsheets,
and executable programs. If you open a binary file in Notepad or TextEdit, it will look like scrambled
nonsense, like in Figure.
Page 19
Module 3 Python
➢
➢ There are three steps to reading or writing files in Python.
➢ Both these commands will open the file in “reading plaintext” mode, or read mode for short.
➢ When a file is opened in read mode, Python lets you only read data from the file; you can’t write or
modify it in any way. Read mode is the default mode for files you open in Python.
➢ But if you don’t want to rely on Python’s defaults, you can explicitly specify the mode by passing the
string value 'r' as a second argument to open().
➢ So open('/Users/asweigart/ hello.txt', 'r') and open('/Users/asweigart/hello.txt')
do the same thing.
➢ The call to open() returns a File object.
➢ A File object represents a file on your computer; it is simply another type of value in Python, much
like the lists and dictionaries you’re already familiar with.
Page 20
Module 3 Python
➢ In the previous example, you stored the File object in the variable helloFile. Now, whenever you
want to read from or write to the file, you can do so by calling methods on the File object in
helloFile.
➢
➢ If you think of the contents of a file as a single large string value, the read() method returns the string
that is stored in the file.
➢ Alternatively, you can use the readlines() method to get a list of string values from the file, one
string for each line of text.
➢ For example, create a file named sonnet29.txt in the same directory as hello.txt and write the follow-
ing text in it:
Make sure to separate the four lines with line breaks. Then enter the following into the interactive shell:
➢ Note that each of the string values ends with a newline character, \n , except for the last line of the file.
A list of strings is often easier to work with than a single large string value. Writing to Files
➢ Python allows you to write content to a file in a way similar to how the print() function
o “writes” strings to the screen.
➢ To write onto a fileyou need to open it in “write plaintext” mode or “append plaintext” mode, or write
mode and append mode for short.
➢ Write mode will overwrite the existing file and start from scratch, just like when you overwrite a
variable’s value with a new value.
➢ Pass 'w' as the second argument to open() to open the file in write mode.
➢ Append mode, on the other hand, will append text to the end of the existing file. Pass 'a' as the
second argument to open() to open the file in append mode.
➢ If the filename passed to open() does not exist, both write and append mode will create a new, blank
file.
➢ After reading or writing a file, call the close() method before opening the file again.
➢ Let’s put these concepts together. Enter the following into the inter- active shell:
Page 21
Module 3 Python
➢ First,bacon.txt is opened in write mode. Since there isn’t a bacon.txt yet, Python creates one.
➢ Calling write() on the opened file and passing write() the string argument 'Hello world! /n'
writes the string to the file and returns the number of characters written, including the newline.
➢ Then close the file.
➢ To add text to the existing contents of the file instead of replacing the string we just wrote, we open the
file in append mode.
➢ We write 'Bacon is not a vegetable.' to the file and close it.
➢ Finally, to print the file contents to the screen, we open the file in its default read mode, call read(),
store the resulting File object in content, close the file, and print content.
➢ Note that the write() method does not automatically add a newline character to the end of the string
like the print() function does. You will have to add this character yourself.
➢ To read and write data using the shelve module, you first import shelve.
➢ Call shelve.open() and pass it a filename, and then store the returned shelf value in a variable. You
can make changes to the shelf value as if it were a dictionary.
➢ call close() on the shelf value after storing the data.
Page 22
Module 3 Python
➢ Here, we open the shelf files to check that our data was stored correctly. Entering shelfFile['cats']
returns the same list that we stored earlier, so we know that the list is correctly stored, and we call
close().
➢ Just like dictionaries, shelf values have keys() and values() methods that will return listlike values
of the keys and values in the shelf.
➢ Since these methods return list-like values instead of true lists, you should pass them to the list()
function to get them in list form. Enter the following into the interactive shell:
➢
➢ Plaintext is useful for creating files that you’ll read in a text editor such as Notepad or TextEdit, but if
you want to save data from your Python programs, use the shelve module.
Saving Variables with the pprint.pformat() Function
➢ The pprint.pprint() function will “pretty print” the contents of a list or dictionary to the screen,
while the pprint.pformat() function will return this same text as a string instead of printing it.
➢ Not only is this string formatted to be easy to read, but it is also syntactically correct Python code. Say
you have a dictionary stored in a variable and you want to save this variable and its contents for future
use.
➢ Using pprint.pformat() will give you a string that you can write to .py file. This file will be your
very own module that you can import when- ever you want to use the variable stored in it.
➢ For example, enter the following into the interactive shell:
Page 23
Module 3 Python
➢ Here, we import pprint to let us use pprint.pformat(). We have a list of dictionaries, stored in a
variable cats.
➢ To keep the list in cats available even after we close the shell, we use pprint.pformat() to return
it as a string. Once we have the data in cats as a string, it’s easy to write the string to a file, which
we’ll call myCats.py.
➢ The modules that an import statement imports are themselves just Python scripts. When the string
from pprint.pformat() is saved to a .py file, the file is a module that can be imported just like any
other.
➢ And since Python scripts are themselves just text files with the .py file extension, your Python programs
can even generate other Python programs. You can then import these files into scripts.
.
➢ The benefit of creating a .py file (as opposed to saving variables with the shelve module) is that because it is a
text file, the contents of the file can be read and modified by anyone with a simple text editor.
Page 24
Module 3 Python
o
➢ Since this program will be randomly ordering the questions and answers, you’ll need to import the
random module ❶ to make use of its functions. The capitals variable ❷ contains a dictionary
with US states as keys and their capi tals as values. And since you want to create 35 quizzes, the code
that actu-ally generates the quiz and answer key files (marked with TODO comments for now) will go
inside a for loop that loops 35 times ❸. (This number can be changed to generate any number of
quiz files.)
➢ Step 2: Create the Quiz File and Shuffle the Question Order
➢ Now it’s time to start filling in those TODOs.
➢ Add the following lines of code to randomQuizGenerator.py:
Page 25
Module 3 Python
▪
➢ The filenames for the quizzes will be capitalsquiz<N>.txt, where <N> is a unique number for the
quiz that comes from quizNum, the for loop’s coun- ter. The answer key for capitalsquiz<N>.txt
will be stored in a text file named capitalsquiz_answers<N>.txt. Each time through the loop, the
%s placeholder in 'capitalsquiz%s.txt' and 'capitalsquiz_answers%s.txt' will be
replaced by (quizNum + 1), so the first quiz and answer key created will be capitalsquiz1.txt
and capitalsquiz_answers1.txt.
➢ These files will be created with calls to the open() function at ❶ and ❷, with 'w' as the second
argument to open them in write mode.
➢ The write() statements at ❸ create a quiz header for the student to fill out. Finally, a
randomized list of US states is created with the help of the random.shuffle() function ❹,
which randomly reorders the values in any list that is passed to it.
➢ The correct
answer is easy
to get—it’s
stored as a
value in the
capitals
Page 26
Module 3 Python
dictionary ❶. This loop will loop through the states in the shuffled states list, from states[0] to
states[49], find each state in capitals, and store that state’s corresponding capital in
correctAnswer. The list of possible wrong answers is trickier. You can get it by duplicating all the
values in the capitals dictionary ❷, deleting the correct answer ❸, and selecting three random
values from this list ❹. The random.sample() func tion makes it easy to do this selection. Its first
argument is the list you want to select from; the second argument is the number of values you want to
select. The full list of answer options is the combination of these three wrong answers with the correct
answers ❺. Finally, the answers need to be randomized ❻ so that the correct response isn’t always
choice D.
➢
➢ A for loop that goes through integers 0 to 3 will write the answer options in the answerOptions list
❶. The expression 'ABCD'[i] at ❷ treats the string 'ABCD' as an array and will evaluate to
'A','B', 'C', and then 'D' on each respective iteration through the loop.
o In the final line ❸, the expression answerOptions.index(correctAnswer) will find the
integer index of the correct answer in the randomly ordered answer options, and
➢ 'ABCD'[answerOptions.index(correctAnswer)] will evaluate to the correct answer’s letter to be
written to the answer key file.
o After you run the program, this is how your capitalsquiz1.txt file will look, though of course
your questions and answer options may be different from those shown here, depending on
the outcome of your random.shuffle() calls:
Page 27
Module 3 Python
▪
➢ The corresponding capitalsquiz_answers1.txt text file will look like this:
Project: Multiclipboard
➢ Say you have the boring task of filling out many forms in a web page or soft- ware with several text
fields.
➢ The clipboard saves you from typing the same text over and over again.
➢ But only one thing can be on the clipboard at a time. If you have several different pieces of text that you
need to copy and paste, you have to keep highlighting and copying the same few things over and over
again.
➢ You can write a Python program to keep track of multiple pieces of text. This
o “multiclipboard” will be named mcb.pyw (since “mcb” is shorter to type than “multiclipboard”).
➢ The .pyw extension means that Python won’t show a Terminal window when it runs this program.The
program will save each piece of clipboard text under a keyword.
➢ For example, when you run py mcb.pyw save spam, the current contents of the clipboard will be saved
with the keyword spam.
➢ This text can later be loaded to the clipboard again by running py mcb.pyw spam. And if the user forgets
what keywords they have, they can run py mcb.pyw list to copy a list of all keywords to the clipboard.
Here’s what the program does:
➢ The command line argument for the keyword is checked.
➢ If the argument is save, then the clipboard contents are saved to the keyword.
➢ If the argument is list, then all the keywords are copied to the clipboard.
➢ Otherwise, the text for the keyword is copied to the keyboard. This means the code will need to do the
following:
➢ Read the command line arguments from sys.argv.
➢ Read and write to the clipboard.
Page 28
Module 3 Python
➢ It’s common practice to put general usage information in comments at the top of the file ❶. If you
ever forget how to run your script, you can always look at these comments for a reminder. Then you
import your modules ❷. Copying and pasting will require the pyperclip module, and reading the
command line arguments will require the sys module. The shelve module will also come in handy:
Whenever the user wants to save a new piece of clipboard text, you’ll save it to a shelf file. Then,
when the user wants to paste the text back to their clipboard, you’ll open the shelf file and load it
back into your program. The shelf file will be named with the prefix mcb ❸.
Step 2: Save Clipboard Content with a Keyword
➢ The program does different things depending on whether the user wants to save text to a keyword,
load text into the clipboard, or list all the existing keywords. Let’s deal with that first case. Make your
code look like the following:
➢ If the first command line argument (which will always be at index 1 of the sys.argv list) is 'save' ❶, the
second command line argument is the keyword for the current content of the clipboard. The keyword
will be used as the key for mcbShelf, and the value will be the text currently on the clip- board ❷.
➢ If there is only one command line argument, you will assume it is either 'list' or a keyword to load content
onto the clipboard. You will implement that code later. For now, just put a TODO comment there ❸.
Page 29
Module 3 Python
➢ If there is only one command line argument, first let’s check whether it’s 'list' ❶. If so, a string
representation of the list of shelf keys will be cop- ied to the clipboard ❷. The user can paste this list
into an open text editor to read it.
➢ Otherwise, you can assume the command line argument is a keyword. If this keyword exists in the
mcbShelf shelf as a key, you can load the value onto the clipboard ❸.
➢ Recall the password locker program you created , that stored the passwords in a dictionary. Updating
the passwords required changing the source code of the program. This isn’t ideal because average users
don’t feel comfortable changing source code to update their software. Also, every time you modify the
source code to a program, you run the risk of accidentally introducing new bugs. By storing the data
for a program in a different place than the code, you can make your programs easier for others to use
and more resistant to bugs.
Page 30